]> sipb.mit.edu Git - ikiwiki.git/commitdiff
* Add templatefile hook.
authorjoey <joey@0fa5a96a-9a0e-0410-b3b2-a0fd24251071>
Thu, 26 Jul 2007 20:50:55 +0000 (20:50 +0000)
committerjoey <joey@0fa5a96a-9a0e-0410-b3b2-a0fd24251071>
Thu, 26 Jul 2007 20:50:55 +0000 (20:50 +0000)
* Add pagetemplate plugin, which allows changing the template used for
  a page. (Not to be confused with the hook of the same name..)

IkiWiki/Plugin/pagetemplate.pm [new file with mode: 0644]
IkiWiki/Plugin/skeleton.pm
IkiWiki/Render.pm
debian/changelog
doc/plugins/pagetemplate.mdwn [new file with mode: 0644]
doc/plugins/write.mdwn
doc/todo/Allow_per-page_template_selection.mdwn
doc/wikitemplates.mdwn

diff --git a/IkiWiki/Plugin/pagetemplate.pm b/IkiWiki/Plugin/pagetemplate.pm
new file mode 100644 (file)
index 0000000..b5ebf62
--- /dev/null
@@ -0,0 +1,40 @@
+#!/usr/bin/perl
+package IkiWiki::Plugin::pagetemplate;
+
+use warnings;
+use strict;
+use IkiWiki 2.00;
+
+my %templates;
+
+sub import { #{{{
+       hook(type => "preprocess", id => "pagetemplate", call => \&preprocess);
+       hook(type => "templatefile", id => "pagetemplate", call => \&templatefile);
+} # }}}
+
+sub preprocess (@) { #{{{
+       my %params=@_;
+
+       if (! exists $params{template} ||
+           $params{template} !~ /^[-A-Za-z0-9._+]+$/ ||
+           ! defined IkiWiki::template_file($params{template})) {
+                return "[[pagetemplate ".gettext("bad or missing template")."]]";
+       }
+
+       if ($params{page} eq $params{destpage}) {
+               $templates{$params{page}}=$params{template};
+       }
+
+} # }}}
+
+sub templatefile (@) { #{{{
+       my %params=@_;
+
+       if (exists $templates{$params{page}}) {
+               return $templates{$params{page}};
+       }
+       
+       return undef;
+} # }}}
+
+1
index fb4cfe9afada4bc62398f32b208cb9c1490b2ec8..d1a50c03bd14fefe093e5064ec9f51848fb7bd04 100644 (file)
@@ -18,6 +18,7 @@ sub import { #{{{
        hook(type => "sanitize", id => "skeleton", call => \&sanitize);
        hook(type => "format", id => "skeleton", call => \&format);
        hook(type => "pagetemplate", id => "skeleton", call => \&pagetemplate);
        hook(type => "sanitize", id => "skeleton", call => \&sanitize);
        hook(type => "format", id => "skeleton", call => \&format);
        hook(type => "pagetemplate", id => "skeleton", call => \&pagetemplate);
+       hook(type => "templatefile", id => "skeleton", call => \&templatefile);
        hook(type => "delete", id => "skeleton", call => \&delete);
        hook(type => "change", id => "skeleton", call => \&change);
        hook(type => "cgi", id => "skeleton", call => \&cgi);
        hook(type => "delete", id => "skeleton", call => \&delete);
        hook(type => "change", id => "skeleton", call => \&change);
        hook(type => "cgi", id => "skeleton", call => \&cgi);
@@ -86,6 +87,13 @@ sub pagetemplate (@) { #{{{
        debug("skeleton plugin running as a pagetemplate hook");
 } # }}}
 
        debug("skeleton plugin running as a pagetemplate hook");
 } # }}}
 
+sub templatefile (@) { #{{{
+       my %params=@_;
+       my $page=$params{page};
+       
+       debug("skeleton plugin running as a templatefile hook");
+} # }}}
+
 sub delete (@) { #{{{
        my @files=@_;
 
 sub delete (@) { #{{{
        my @files=@_;
 
index 911e9c273f978d49486c176dc37c71c01ed64c6b..a95da40d253f8650671fd843cf5ee858bbfd42cc 100644 (file)
@@ -69,7 +69,15 @@ sub genpage ($$$) { #{{{
        my $content=shift;
        my $mtime=shift;
 
        my $content=shift;
        my $mtime=shift;
 
-       my $template=template("page.tmpl", blind_cache => 1);
+       my $templatefile;
+       run_hooks(templatefile => sub {
+               return if defined $templatefile;
+               my $file=shift->(page => $page);
+               if (defined $file && defined template_file($file)) {
+                       $templatefile=$file;
+               }
+       });
+       my $template=template(defined $templatefile ? $templatefile : 'page.tmpl', blind_cache => 1);
        my $actions=0;
 
        if (length $config{cgiurl}) {
        my $actions=0;
 
        if (length $config{cgiurl}) {
index 0ec9139574885897c94e2f1c07fcb11303460bbe..5a1d94a691cbd0ff5132a6ce763a5f8615b8f244 100644 (file)
@@ -14,8 +14,11 @@ ikiwiki (2.5) UNRELEASED; urgency=low
   * Fix re-rendering of pages when a linked to page goes away. This was broken
     in version 1.50 with the addition of %destsources. Thanks, Ethan
     Glasser-Camp.
   * Fix re-rendering of pages when a linked to page goes away. This was broken
     in version 1.50 with the addition of %destsources. Thanks, Ethan
     Glasser-Camp.
+  * Add templatefile hook.
+  * Add pagetemplate plugin, which allows changing the template used for
+    a page. (Not to be confused with the hook of the same name..)
 
 
- -- Joey Hess <joeyh@debian.org>  Thu, 26 Jul 2007 15:30:22 -0400
+ -- Joey Hess <joeyh@debian.org>  Thu, 26 Jul 2007 16:16:55 -0400
 
 ikiwiki (2.4) unstable; urgency=low
 
 
 ikiwiki (2.4) unstable; urgency=low
 
diff --git a/doc/plugins/pagetemplate.mdwn b/doc/plugins/pagetemplate.mdwn
new file mode 100644 (file)
index 0000000..192322d
--- /dev/null
@@ -0,0 +1,16 @@
+[[template id=plugin name=pagetemplate author="[[Joey]]"]]
+[[tag type/chrome]]
+
+This plugin allows a page to be created using a different
+[[template|wikitemplate]]. The page text is inserted into the template, so
+the template controls the overall look and feel of the wiki page. This is
+in contrast to the [[template]] plugin, which allows inserting templates
+_into_ the body of a page.
+
+This plugin can only use templates that are already installed in
+/usr/share/ikiwiki/templates (or wherever ikiwiki is configured to look for
+them). You can choose to use any .tmpl files in that directory. Example:
+
+       \[[pagetemplate template=my_fancy.tmpl]]
+       
+[[pagetemplate template=my_fancy.tmpl]]
index 870b3c451e24faea3a403a3b08c8e9ae8bda80f2..a122845af470fa4f40c46b5ad8caf93900590ebd 100644 (file)
@@ -128,17 +128,28 @@ return the htmlized content.
 
        hook(type => "pagetemplate", id => "foo", call => \&pagetemplate);
 
 
        hook(type => "pagetemplate", id => "foo", call => \&pagetemplate);
 
-[[Templates]] are filled out for many different things in ikiwiki, like
-generating a page, or part of a blog page, or an rss feed, or a cgi. This
-hook allows modifying those templates. The function is passed named
-parameters. The "page" and "destpage" parameters are the same as for a
-preprocess hook. The "template" parameter is a [[cpan HTML::Template]]
-object that is the template that will be used to generate the page. The
-function can manipulate that template object.
+[[Templates|wikitemplate]] are filled out for many different things in
+ikiwiki, like generating a page, or part of a blog page, or an rss feed, or
+a cgi. This hook allows modifying the variables available on those
+templates. The function is passed named parameters. The "page" and
+"destpage" parameters are the same as for a preprocess hook. The "template"
+parameter is a [[cpan HTML::Template]] object that is the template that
+will be used to generate the page. The function can manipulate that
+template object.
 
 The most common thing to do is probably to call `$template->param()` to add
 a new custom parameter to the template.
 
 
 The most common thing to do is probably to call `$template->param()` to add
 a new custom parameter to the template.
 
+### templatefile
+
+       hook(type => "templatefile", id => "foo", call => \&templatefile);
+
+This hook allows plugins to change the [[template|wikitemplate]] that is
+used for a page in the wiki. The hook is passed a "page" parameter, and
+should return the name of the template file to use, or undef if it doesn't
+want to change the default ("page.tmpl"). Template files are looked for in
+/usr/share/ikiwiki/templates by default.
+
 ### sanitize
 
        hook(type => "sanitize", id => "foo", call => \&sanitize);
 ### sanitize
 
        hook(type => "sanitize", id => "foo", call => \&sanitize);
index 4d3041d2a0f75cf84be8f802b27bd9666fc5bd08..231ccf502338ceb60997f8f6e960d50c5cb1cbca 100644 (file)
@@ -39,3 +39,5 @@ could be accepted upstream. Joey, what do you think? And would a
 proper patch be helpful (it really just adds those lines to the right
 place).
 
 proper patch be helpful (it really just adds those lines to the right
 place).
 
+> Fully implemented as a templatefile hook and a [[plugins/pagetemplate]]
+> plugin. --[[Joey]] [[done]]
index 5e2eb8a3769a392371f51b27dd4af1b65a34cfdd..4fc163599ac67851d989e935ec3d723a2fb04903 100644 (file)
@@ -34,5 +34,8 @@ located in /usr/share/ikiwiki/templates by default.
 * `searchform.tmpl` - Used by the [[plugins/search]] plugin to add a search
   form to wiki pages.
 
 * `searchform.tmpl` - Used by the [[plugins/search]] plugin to add a search
   form to wiki pages.
 
+The [[plugins/pagetemplate]] plugin can allow individual pages to use a
+different template than `page.tmpl`.
+
 The [[plugins/template]] plugin also uses templates, though those
 [[templates]] are stored in the wiki.
 The [[plugins/template]] plugin also uses templates, though those
 [[templates]] are stored in the wiki.