[pmwiki-users] Feature request - selecting between multiple templates for new pages

Eemeli Aro eemeli at gmail.com
Tue Feb 10 07:24:23 CST 2009

2009/2/9 Steven Benmosh <wordz2u at gmail.com>:
> The feature: I would like to be able to choose between several (existing)
> templates when I create a new page in my wiki.

I've implemented exactly this on my sites, as an EditForm GUIButton
that pops up a small dialog with a selection of templates that can be
used, generated from a PageList. I don't really have the time or
interest just now to properly implement this as a cookbook, but here's
the rough version.

The general idea is to use a custom-formatted pagelist to list and
link to using all the available templates on the edit form if the edit
engine will allow a template to be used, ie. if the contents of the
page are blank.

It's probably possible to do this without any special PHP, but as this
works I haven't bothered yet. The tricky bit would probably be
checking whether the current page is empty. If you're not bothered
about linking to templates even when they can't be used or having a
nice dialog-y interface, you can probably get away with adding just
the following to Site.EditForm:

(:pagelist group=Templates name=-Templates list=normal fmt=#templatelinks :)
(:if false:)
* [[{*$FullName}?action=edit&template={=$FullName}|{=$Name}]]

For the full magic, you'll need to first add the following to your
PmWiki config file or to a file that you include_once from a config
file (probably inside an if($action=='edit') block). Beware, GMail's
automatic line breaks will probably mangle something here.

Markup( 'templatelinks', 'directives', '/\\(:templatelinks:\\)/e',
"LinkToTemplates(\$pagename)" );

function LinkToTemplates( $pagename ) {
	$head = "<div class='editbutton' title='To use a template...'
onclick=\"document.getElementById('templatesbox').style.display =
'block';\"><span>Templates</span></div>\n<div id='templatesbox'
onclick=\"document.getElementById('templatesbox').style.display =
	$tail = "\n<div align='right'
style='font-style:italic;font-size:0.75em;'>click to

	$page = RetrieveAuthPage($pagename, 'edit', false);
	if (!empty( $page['text'] )) return Keep( "{$head}To use a <a
href='/Templates'>template</a>, clear the title and text fields of the
page, save it and re-edit it.$tail" );

	$str = FmtPageList( '$MatchList', $pagename, array( 'o' => '
group=Templates name=-Templates list=normal fmt=#templatelinks ' ) );
	if ( $str > '' ) return Keep("{$head}Use a <a

Next, add the following to your Site.LocalTemplates:

* [[{*$FullName}?action=edit&template={=$FullName}|{=$Name}]]

The above will enable the use of the markup directive
(:templatelinks:) which you ought to add to your Site.EditForm page,
probably just before the (:e_guibuttons:) directive. To customize it,
you'll need to edit the above PHP directly as it isn't in any way
parameterised and includes hardcoded links to '/Templates', which in
all likelihood is incorrect fro your site. As such, the script will
look for pages in a group called Templates except for

To make the results look nicer (and possibly work at all...), you
probably should add something like the following to your CSS:

#content div.editbutton {
	float: right;
	margin: 0 -5px 0 10px;
	color: #000;
	cursor: pointer;
	height: 22px;
	background: transparent url('images/button-stretch.png') no-repeat;
#content div .editbutton span {
	display: block;
	padding: 5px;
	line-height: 12px;
	background: transparent url('images/button-right.png') no-repeat top right;
#content div.editbox {
	display: none;
	float: right;
	position: absolute;
	z-index: 1;
	right: -5px;
	width: 150px;
	padding: 5px 5px 0 5px;
	border: 1px solid #cccccc;
	background-color: #f9f9f9;
	cursor: pointer;

The referred-to images you can grab from
<http://autsys.tkk.fi/css/images/button-right.png> and
<http://autsys.tkk.fi/css/images/button-stretch.png> -- save them
somewhere appropriate and adjust the CSS accordingly. I honestly can't
remember if I made those images myself or grabbed them from another
recipe, so apologies if attribution is missing.

Hope this helps you or someone else sometime...


More information about the pmwiki-users mailing list