Back to documentation
# PODNAME: Statocles::Help::Theme
# ABSTRACT: A guide to making Statocles themes


A Theme is the "View" class of Statocles. Themes build
L<Statocles::Template|Statocles::Template> objects that apps can then attach to
a page. Each template has a category and a name. The category usually maps to
the application name, or "site" for global site templates. Themes get their
templates from a L<Statocles::Store|Statocles::Store>.

=head1 Default Themes

There are some default themes included with Statocles, with more on the way.
To use a default theme, set your site's theme to C<::name>, where C<name> is
the name of the theme. The currently-available default themes are:

=over 4

=item C<::default>

The default theme uses L<the Skeleton CSS boilerplate|>
to build a simple, clean starting point for your own theme.

=item C<::bootstrap>

The bootstrap theme uses L<the Bootstrap library|> to
provide an elegant and flexible theme. Sites like L<Bootswatch|>
can provide a custom look to your Bootstrap site.


=head1 Making Minor Adjustments

=head2 Include Stores

Before going to editing the entire theme, you can change the theme includes
by adding L<include_stores|Statocles::Theme/include_stores>.

Some examples of files you can edit are:

=over 4

=item site/head_after.html.ep

This include is after all the default C<E<lt>headE<gt>> content. You can add
links to new scripts and stylesheets here.

=item site/header_after.html.ep

This include is after the nav bar and page header. You can add banners and such

=item site/footer.html.ep

The entire site footer is up to you to edit.

=item site/sidebar_before.html.ep

If the page has a sidebar, this include will be placed before it.


=head1 Editing a Theme

=head2 Start From the Default Theme

It is unlikely that the default themes will do things exactly as you need. So,
Statocles has a command to copy one of the default themes into your site
directory so that you can edit it as you need.

    # Bundle the default theme
    $ statocles bundle theme default site/theme
    Theme "default" written to "site/theme"
    Make sure to update "site.yml"

This command will copy the "default" Statocles theme into the "site/theme" directory.
Now we need to update our C<site.yml> to point to our new theme:

    # site.yml
        class: Statocles::Site
            theme: site/theme

=head1 Writing a Template

The default L<Statocles::Theme|Statocles::Theme> uses
L<Mojo::Template|Mojo::Template>, the template class from
L<Mojolicious|Mojolicious>. In the template, there are a few directives that
allow you to evaluate Perl code, which is why the templates are also called
".ep" for Embedded Perl.

    # A single leading % means the rest of the line is Perl
    % if ( $self->path eq '/index.html' ) {

    # A leading %= means replace with the return value
    %= $self->path

    # Inline code can be wrapped in <% ... %>
    # <%= ... %> is replaced with the return value
    My name is <%= $self->name %>

    # Comments are %# and <%# %>
    %# This is in the template, but not the result
    <%# A comment %>

=head2 Template Variables

When an application assembles a L<page object|Statocles::Page>, it sets values
inside. The page then gives those values to the template. The common values in
every template are:

=over 4

=item C<$self> - The current L<Statocles::Page|Statocles::Page> object

=item C<$site> - The current L<Statocles::Site|Statocles::Site> object

=item C<$app> - The current L<Statocles::App|Statocles::App> object


These objects hold all the data we need to render the page. Since you are writing Perl,
you can use any of the methods from those objects. Some useful methods to note are:

=over 4

=item C<data()>

All of the objects above, Page, Site, and App, have a C<data()> method that
allows for an arbitrary hash of key/values. Site and App data are configured in the Site's
config file, so these can be used as hooks for template authors to add
additional features, such as social media links or metrics tools. Page objects
data come from the App, and add information that may not fit anywhere else.

=item L<C<$self-E<gt>sections>|Statocles::Page::Document/sections>

The C<sections> method works on L<document pages|Statocles::Page::Document> and gives
you all of the content split into sections on the section marker (C<--->).

    # Get only the first section
    my ( $first_section ) = $self->sections;

    # Loop over the sections
    % for my $section ( $self->sections ) {
        <%= $section %>
    % }


=head2 Helpers

Helpers are extra functions available to the template.

=over 4

=item include

The C<include> helper allows you to include another template or file. The include
will be searched for in the theme directory.

    # Include a template, passing the same variables as the current template
    %= include "site/before_header.html.ep"

    # Include a file without template processing
    %= include "site/before_header.html"

If the included path ends with ".ep", it is treated as a template. Otherwise,
it's just written directly with no processing.

=item markdown

The C<markdown> helper allows you to render Markdown to HTML. This is useful
when you're using extra C<data()> (see above) or for things like L<the blog app
tag_text attribute|Statocles::App::Blog/tag_text>.

    # Render Markdown as HTML
    %= markdown $self->data->{markdown}


=head1 Layouts

When a page object is built, it is given a template and a layout. The default layout is
a special template called C<default.html.ep> in the C<layout> category that wraps
the content from the template, allowing a consistent site style in a single

The layout generally adds the site's scaffolding: <html>, <head>, <body>,
scripts and stylesheets, header and footer.

The layout gets the exact same template variables that the current page's
template gets, so you have the current page (C<$self>), current site
(C<$site>), and current app (C<$app>), in case you need it for the layout.

=head1 Stylesheets and Scripts

A site's theme is deployed to the "/theme" path. If a theme needs extra files,
like stylesheets, scripts, and images, they can be added to the theme's
directory and referenced in the HTML from "/theme".

    # Reference a stylesheet in theme/css/normalize.css
    <link rel="stylesheet" href="/theme/css/normalize.css" />

=head1 SEE ALSO

=over 4

=item L<Statocles::Theme>

=item L<Statocles::Template>