EditTemplates

Summary: Specify a wiki page or pages to use as a template when a new page is created.
Status: Stable
Version: N/a
Prerequisites: pmwiki-2.0.0
Maintainer: Petko
Discussion: EditTemplates-Talk
Users: +7 (View / Edit)

Description

The $EditTemplatesFmt variable specifies the name of a wiki page to be used as a template for "new" pages. For example:

$EditTemplatesFmt[] = 'Cookbook.Template';

will say to use the page Cookbook.Template as the template for all new pages. $EditTemplatesFmt can contain references to $Group, $Name, etc., so

$EditTemplatesFmt[] = '{$Group}.Template';

says to use 'Template' within the current group as the template for new pages.

Another possibility is

$EditTemplatesFmt[] = '{$SiteGroup}.{$Name}Template';

which looks in the Site group for a template matching the name of the page being created. For example, the above would use "Site.HomePageTemplate" (if it exists) anytime an author creates a page named "HomePage" (in any group).

An administrator can also specify an array of templates, in which case the first template found is chosen:

# use 'Template' in the current group if it exists, or the default group if it exists,
# otherwise use 'Main.MasterTemplate'
$EditTemplatesFmt = array('{$Group}.Template','{$DefaultGroup}.Template','Main.MasterTemplate');

You can define pagename patterns for a template, so that the template is loaded only when the page name matches the first pattern found:

# Use MapTemplate for new pages where the name ends with "map" or "maps"
$EditTemplatesFmt[] = 'Site.MapTemplate name=*map,*maps';

# Use BlogTemplate for new pages in the Blog group
$EditTemplatesFmt[] = 'Site.BlogTemplate name=Blog.*';

The name= patterns are case insensitive and work like in pagelists and conditionals.

One-Shot Templates

If you wish to create a new page based on the contents of an older page, without going through the effort to set it up as a template, then you can simply add the template= parameter to the end of the URL, like this:

[[SomeGroup.NewPage?action=edit&template=OldGroup.OldPage]]

which will produce a link that, when clicked on, will open the NewPage using the contents of OldPage as the default.

Single Page Templates

It is possible to modify a single page so that all links from it will use a common template. To modify the page SomeGroup.SomePage so all of the links within it will use the file SomeGroup.SomeTemplate as the edit template, create a file in the local directory named SomeGroup.SomePage.php containing the following php code:

<?
$LinkPageCreateFmt = "<a class='createlinktext'
href='\$PageUrl?action=edit&amp;template=SomeGroup.SomeTemplate'>\$LinkText</a>
<a class='createlink' href='\$PageUrl?action=edit&amp;template=SomeGroup.SomeTemplate'>?</a>";

This changes the "edit new page" links in SomeGroup.SomePage to automatically include the template= parameter.

Select among a number of templates

The recipe EditTemplatesMenu allows wiki authors a choice among several pre-filled templates before creating new pages.

Automated replacements of text in the templates

Sometimes you may want to have some text, title, or summary in the template to help editors know what the template is about, but when a new page is created, you may want to remove this template-specific markup, or replace it to remind people to use it in the new page.

You can do this by defining custom $TROEPatterns entries:

# Replace the template title, remind to change it
$TROEPatterns['/\\(:title .*?:\\)/i'] = "(:title Type the title of the page here:)";

# Remove the template summary that appears in listings
$TROEPatterns['/^Summary:.*$/m'] = "";

# Keep at most 2 consecutive newlines
$TROEPatterns["/\n\n{3,}/"] = "\n\n";

See ROSPatterns for more information.

The Template Shuffle

It is also possible to use different templates in different sections of a page by making use of PmWiki's Markup() function to create a new directive, (:edittemplate:), which sets the template of all links which follow it. For example, assume you are setting up page with two groups of links, one to pages about movies, and one to pages about TV shows, and you wish those links to use edit templates Main.MovieTemplate and Main.TVTemplate, respectively. By placing (:edittemplate Main.MovieTemplate:) at the start of the movie links, and then placing (:edittemplate Main.TVTemplate:) at the start of the TV show links, each group will be set up to use the appropriate templates.

To make enable this capability, add the following to local/config.php:

# change the link format for creating new pages
$LinkPageCreateFmt = "<a class='createlinktext'
href='\$PageUrl?action=edit\$LinkTemplate'>\$LinkText</a>
<a class='createlink' href='\$PageUrl?action=edit\$LinkTemplate'>?</a>";

# default is no template
$FmtV['$LinkTemplate'] = '';

# the (:edittemplate Group.PageName:) markup causes any create page 
# links that follow to use Group.PageName as the template
Markup('edittemplate', 'directives',
'/\\(:edittemplate(.*?):\\)/',
"MarkupEditTemplateLinkTemplate");

function MarkupEditTemplateLinkTemplate($m) {
  $t = trim($m[1]);
  $GLOBALS['FmtV']['$LinkTemplate'] = $t ? '&amp;template=' . $t : '';
}

If after a specific section you want to return to the default new page behavior, simply insert (:edittemplate:) with no page reference, and the previously set template will be cleared.

Simple form for creating new pages

This is a simple form for creating a page using a template.

(:input form "{$ScriptUrl}":)(:input hidden action edit:)
(:input hidden template MyTemplateGroup.MyTemplate:)
Please enter new page title: (:input text pagename size=32:)
(:input submit create "Create!":)
(:input end:)

Please enter new page title:

When the editor fills the new page title and presses "Create!", he sees a new page where the textarea is not empty but filled with the content of the page MyTemplateGroup.MyTemplate.

See also the more advanced recipes New page box plus and New group box.

See Also

  • Please note that various cookbook recipes also have methods to edit a new page based on a template.
  • EditTemplatesMenu allows wiki authors a choice among several pre-filled templates before creating new pages.

Contributors

  • Petko, current maintainer
  • Pm, 2005-03-05

Comments

See discussion at EditTemplates-Talk

User notes +7: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.