PostFooter.pm 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package Trog::Component::PostFooter;
  2. use 5.040;
  3. use re '/aasx';
  4. use warnings FATAL => 'all';
  5. use Trog::Component ();
  6. use Trog::Themes;
  7. =head1 Trog::Component::PostFooter
  8. The per-post footer a series can name in its metadata, drawn from the footers/
  9. directory of the stock templates or the theme's.
  10. =head1 FUNCTIONS
  11. =head2 render(%args) = STRING
  12. Render footers/$args{name}.
  13. The name comes out of post data, so it is checked against the footers/ listing
  14. before it becomes a path -- see available(). Returns the empty string when no
  15. name is given, so that the caller can ask unconditionally.
  16. Beyond the name, the template gets the post it belongs to and the route it is
  17. being shown at, plus theme_dir for building asset paths. That is narrower than
  18. the whole request hash these used to be rendered with; a theme whose footer
  19. wants something else should be handed it at the call site in posts.tx.
  20. =cut
  21. sub render (%args) {
  22. my $name = delete $args{name};
  23. return '' unless $name;
  24. die "No such footer '$name'\n" unless grep { $_ eq $name } @{ available() };
  25. $args{theme_dir} //= Trog::Themes::td();
  26. return Trog::Component::render_template( "footers/$name", \%args );
  27. }
  28. =head2 available() = ARRAYREF
  29. The footers a post may name: every .tx in footers/, the theme's and the stock ones
  30. both. This is the same list the series edit form offers, so anything the form
  31. can pick renders, and nothing else does.
  32. =cut
  33. sub available () {
  34. return Trog::Themes::themed_templates_in_dir( 'footers', 'text/html', 1 );
  35. }
  36. 1;