PostFooter.pm 1.5 KB

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