Skip to content

Commit

Permalink
add site pages cache
Browse files Browse the repository at this point in the history
This cache allows template helpers to quickly get at the entire site's page list
so that they can build navigations or produce metadata about the site.
  • Loading branch information
preaction committed Feb 19, 2016
1 parent 742e411 commit c6709d6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/Statocles/Site.pm
Expand Up @@ -339,6 +339,20 @@ has _write_deploy => (
clearer => '_clear_write_deploy',
);

=attr pages
A cache of all the pages that the site contains. This is generated
during the C<build> phase and is available to all the templates
while they are being rendered.
=cut

has pages => (
is => 'rw',
isa => ArrayRef[ConsumerOf['Statocles::Page']],
default => sub { [] },
);

=method BUILD
Register this site as the global site.
Expand Down Expand Up @@ -513,7 +527,9 @@ sub build {
pages => \@pages,
);

# @pages should not change after this, because it is used in the render loop below.
# @pages should not change after this, because it is being cached
$self->pages( \@pages );

$self->emit(
'before_build_write',
class => 'Statocles::Event::Pages',
Expand Down
5 changes: 5 additions & 0 deletions t/site/build_and_deploy.t
Expand Up @@ -41,6 +41,8 @@ sub test_base_url {
}

subtest 'build' => sub {
cmp_deeply $site->pages, [], 'page cache is empty until a build';

$site->build;

my @pages;
Expand All @@ -67,6 +69,9 @@ subtest 'build' => sub {
}
};

# Add 2 pages for robots.txt and sitemap.xml
is scalar @{ $site->pages }, scalar @pages + 2, 'cached page count is correct';

subtest 'build deletes files before building' => sub {
$build_dir->child( 'DELETE_ME' )->spew( "This should be deleted" );
$site->build;
Expand Down

0 comments on commit c6709d6

Please sign in to comment.