Skip to content

Commit

Permalink
use document templates to override page templates
Browse files Browse the repository at this point in the history
When a document has a template or layout set, the page will use that
instead of the template/layout set by the application.

This has revealed a bunch of technical debt around creating and giving
template _objects_ to the Page. Since the Page has the Site which has
the Theme, we don't need to give it objects. We can just give it
arrays/strings and it can figure it out.

Fixes #40
  • Loading branch information
preaction committed May 24, 2015
1 parent c71ca78 commit 303c0a7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/Statocles/Document.pm
Expand Up @@ -109,7 +109,9 @@ has date => (

=attr template
A template override for this document.
A template override for this document. If set, the L<document
page|Statocles::Page::Document> will use this instead of the template provided
by the application.
=cut

Expand All @@ -125,7 +127,9 @@ has template => (

=attr layout
A layout override for this document.
A layout template override for this document. If set, the L<document
page|Statocles::Page::Document> will use this instead of the layout provided
by the application.
=cut

Expand Down
32 changes: 32 additions & 0 deletions lib/Statocles/Page/Document.pm
Expand Up @@ -131,6 +131,38 @@ sub tags {
return @{ $self->_tags };
}

=method template()
The L<template|Statocles::Template> for this page. If the document has a template,
it will be used. Otherwise, the L<template attribute|Statocles::Page/template> will
be used.
=cut

around template => sub {
my ( $orig, $self, @args ) = @_;
if ( $self->document->has_template ) {
return $self->site->theme->template( @{ $self->document->template } );
}
return $self->$orig( @args );
};

=method layout()
The L<layout template|Statocles::Template> for this page. If the document has a layout,
it will be used. Otherwise, the L<layout attribute|Statocles::Page/layout> will
be used.
=cut

around layout => sub {
my ( $orig, $self, @args ) = @_;
if ( $self->document->has_layout ) {
return $self->site->theme->template( @{ $self->document->layout } );
}
return $self->$orig( @args );
};

1;
__END__
Expand Down
27 changes: 26 additions & 1 deletion t/page/document.t
Expand Up @@ -6,7 +6,10 @@ use Statocles::Document;
use Statocles::Page::Document;
use Text::Markdown;
use Statocles::Site;
my $site = Statocles::Site->new( deploy => tempdir );
my $site = Statocles::Site->new(
deploy => tempdir,
theme => $SHARE_DIR->child(qw( theme )),
);

my $doc = Statocles::Document->new(
path => '/path/to/document.markdown',
Expand Down Expand Up @@ -69,6 +72,28 @@ subtest 'page date overridden by published date' => sub {
is $page->date->datetime, $tp->datetime;
};

subtest 'document template/layout override' => sub {
my $doc = Statocles::Document->new(
path => '/path/to/doc.markdown',
title => 'Page Title',
content => 'Page content',
template => '/document/recipe.html',
layout => '/layout/logo.html',
);

my $page = Statocles::Page::Document->new(
path => '/path/to/doc.html',
site => $site,
document => $doc,
template => '<%= $content %>', # will be overridden
layout => '<%= $content %>', # will be overridden
);

my $output = $page->render;
my $expect = "Logo layout\nRecipe template\nPage Title\n<p>Page content</p>\n\n\n";
eq_or_diff $output, $expect;
};

subtest 'template coercion' => sub {

subtest 'template' => sub {
Expand Down
3 changes: 3 additions & 0 deletions t/share/theme/document/recipe.html.ep
@@ -0,0 +1,3 @@
Recipe template
%= $self->title
%= $content
2 changes: 2 additions & 0 deletions t/share/theme/layout/logo.html.ep
@@ -0,0 +1,2 @@
Logo layout
<%= $content %>

0 comments on commit 303c0a7

Please sign in to comment.