Header.pm 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package Trog::Component::Header;
  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::Header
  8. Everything from the doctype down to the opening body tag: the title, the social
  9. and icon metadata, and the stylesheet and script links.
  10. =head1 FUNCTIONS
  11. =head2 render(%args) = STRING
  12. Render header.tx.
  13. It takes these arguments, all optional, and dies on any other:
  14. title / route / lang / author the page itself
  15. meta_desc / meta_tags / default_tags social metadata, built by the route
  16. stylesheets / print_styles / scripts resolved paths, built by finish_render
  17. embed embedded view, which adds a base target
  18. no_doctype leave the doctype off, for the RSS
  19. stylesheet, which is XSL rather than HTML
  20. title_select in place of title, the XPath of the
  21. title, for that same stylesheet
  22. The list arguments default to empty rather than to the stock sheets, so that a
  23. caller which has none -- rss_style(), notably -- gets a header with none, as it
  24. did when the route rendered this itself.
  25. theme_dir is worked out here when it is not passed, as it is the same for every
  26. page and the template only wants it to build asset paths.
  27. =cut
  28. my @arguments = qw{title route lang author meta_desc meta_tags default_tags stylesheets print_styles scripts embed no_doctype title_select theme_dir};
  29. my %known = map { $_ => 1 } @arguments;
  30. sub render (%args) {
  31. my @unknown = grep { !$known{$_} } sort keys(%args);
  32. die "The Header component takes no argument named @unknown\n" if @unknown;
  33. $args{$_} //= undef for @arguments;
  34. $args{lang} //= 'en-US';
  35. $args{title} //= 'tCMS';
  36. $args{stylesheets} //= [];
  37. $args{print_styles} //= [];
  38. $args{scripts} //= [];
  39. # td() answers a rooted URL path, which for a themed site starts /www/ --
  40. # the template builds hrefs with it, and /www is not in the URL space.
  41. my $theme_dir = Trog::Themes::td();
  42. $theme_dir =~ s|^/www/||;
  43. $args{theme_dir} //= $theme_dir;
  44. return Trog::Component::render_template( 'header.tx', \%args );
  45. }
  46. 1;