summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/template.pm
blob: 3d088289de667f00651739be80268ba6316a51fe (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. hook(type => "preprocess", id => "template", call => \&preprocess);
  11. } # }}}
  12. sub preprocess (@) { #{{{
  13. my %params=@_;
  14. if (! exists $params{id}) {
  15. return "[[template missing id parameter]]"
  16. }
  17. my $template_page="templates/$params{id}";
  18. add_depends($params{page}, $template_page);
  19. my $template_file=$pagesources{$template_page};
  20. return "[[template ".
  21. htmllink($params{page}, $params{destpage}, $template_page).
  22. " not found]]"
  23. unless defined $template_file;
  24. my $template=HTML::Template->new(
  25. filter => sub {
  26. my $text_ref = shift;
  27. $$text_ref=&Encode::decode_utf8($$text_ref);
  28. chomp $$text_ref;
  29. },
  30. filename => 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