PageTemplate
<< Diff Access Level | Cookbook-V1 | Upload Groups >>
Note: The recipes here are for PmWiki versions 0.6 and 1.0 only. For PmWiki 2.0 recipes, see Cookbook.
Goal
For some Wiki pages, like bug reports, it's good to define a standard structure of the page. Using a page template can help users follow the standard page structure for new pages without having to re-type the page structure or copy-and-paste from other pages.
Solution
The solution is particularly simple.
- Define or decide on a PmWiki/WikiGroup where you want to put your formatted pages. For example, BugReports or Cookbook. All pages in this group will start off with the new template.
- Create a page in the group to act as the page template. A good place to put it is at GroupName/Template.
- Create a PmWiki/PerGroupCustomizations file in the
local
directory for your templated group. Add the following text to the customization file:
<?php $FormTemplatePage = '$Group.Template'; if ($action=="edit") { $formpage = ReadPage(FmtPageName($FormTemplatePage,$pagename)); $DefaultPageTextFmt = $formpage['text']; } ?>
This sets the default page text for new pages to the text of your template page. When users click on a link to start a new page, the edit box will be filled up with the contents of the template page.
Discussion
If you decide to use a fixed name for the template page in all of your groups, you can include the above code in your local/config.php
file instead of in per-group customization files.
Writing good templates is a dark art. It's good to use %%comment%% blocks to give instructions to editors on how to use the template and what kind of information should go where.
See Also
- FormTemplateSystemII -- has a more elaborate and complete solution to making structured pages with HTML forms.
History
- 1 March 2004. The idea came from Development.WikiForms, but I copied it here as a new cookbook entry, since I figured it was really useful. Like, it could be used for the Cookbook itself!
Comments & Bugs
- The code above doesn't check to see if the template page exists, though.
- There's no enforcement of the structure. It's just a suggestion for users when they create a new page. They can delete all the template stuff and just write in whatever they want.
- All the pages in the group have to use the same template.
- It might be nice to let the user choose a template to use.
- The following hack will allow you to specify different templates for individual pages within a group, as long as their titles all match a definable format (I'd imagine the code implementation could be done in a way that scales more elegantly):
<?php $SpecificTemplatePage1 = '$Group.TemplateOne'; $SpecificTemplatePage2 = '$Group.TemplateTwo'; $DefaultTemplatePage = '$Group.DefaultTemplate'; # match page names formatted like Minutes2003-12-03 $template1_pattern = "{$Group}/Minutes[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}"; # match page names formatted like Photos2002-02-03 $template2_pattern = "{$Group}/Photos[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}"; if ("edit" == $action) { if (ereg($template1_pattern, $pagename)) { $formpage = ReadPage(FmtPageName($SpecificTemplatePage1, $pagename)); } elseif (ereg($template2_pattern, $pagename)) { $formpage = ReadPage(FmtPageName($SpecificTemplatePage2, $pagename)); } else { $formpage = ReadPage(FmtPageName($DefaultTemplatePage, $pagename)); } $DefaultPageTextFmt = $formpage['text']; } ?>
Contributors
pmwiki-2.3.38 -- Last modified by {{Petko}}