summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/template.pm
blob: 5b4eeb3a8f66f609f42a6a2f13df0e138c00540f (plain)
  1. #!/usr/bin/perl
  2. # Structured template plugin.
  3. package IkiWiki::Plugin::template;
  4. use warnings;
  5. use strict;
  6. use IkiWiki;
  7. use HTML::Template;
  8. use Encode;
  9. sub import { #{{{
  10. IkiWiki::hook(type => "preprocess", id => "template",
  11. call => \&preprocess);
  12. } # }}}
  13. sub preprocess (@) { #{{{
  14. my %params=@_;
  15. if (! exists $params{id}) {
  16. return "[[template missing id parameter]]"
  17. }
  18. my $template_page="templates/$params{id}";
  19. IkiWiki::add_depends($params{page}, $template_page);
  20. my $template_file=$IkiWiki::pagesources{$template_page};
  21. return "[[template ".
  22. IkiWiki::htmllink($params{page}, $params{destpage}, $template_page).
  23. " not found]]"
  24. unless defined $template_file;
  25. my $template=HTML::Template->new(
  26. filter => sub {
  27. my $text_ref = shift;
  28. $$text_ref=&Encode::decode_utf8($$text_ref);
  29. },
  30. filename => IkiWiki::srcfile($template_file),
  31. die_on_bad_params => 0,
  32. no_includes => 1,
  33. blind_cache => 1,
  34. );
  35. foreach my $param (keys %params) {
  36. $template->param($param => $params{$param});
  37. }
  38. return IkiWiki::preprocess($params{page}, $params{destpage},
  39. $template->output);
  40. } # }}}
  41. 1