Translations of this page?:

Wiki Templates

What I want to do is to have the ability to create a namespace called templates in my wiki. Then whenever I create a new page, it would provide a template dropdown list of all the pages stored in the templates namespace. Clicking on one of them would automatically paste the content of the template wiki page into the edit box of the current page. Then I could create my new document based on that template… I'm going to work on it myself but ideas would be helpful!

Another option would be to have a button on the toolbar that says “Create New Page Based On This One” or “Copy This Page” or something like that.

Posted by Jeff Mikels jeffweb [at] mikels [dot] cc

Something similar to this has recently been implemented in the development version of Dokuwiki. Refer to the mailing list for more details. — ChrisS 2005-07-21
I just implemented it myself as a template. It would be more efficient if I used ajax, but right now it works well if there aren't too many templates.

Source of copyFrom.php

<?php
 
/**
 * Allows to copy source from another wikipage to currently edited wikipage.
 * The dropdown list is generated from files stored in namespace "templates"
 *
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Jeff Mikels <jeffweb [at] mikels [dot] cc>
 */
 
function html_copyFrom() {
	global $ACT;
	//echos code with a dropdown list of all wikipages stored in templates namespace
	if (($ACT != 'edit') && ($ACT != 'preview')) return '';
	cf_showTemplateList();
}
 
 
function cf_getTemplateIds(){
	require_once(DOKU_INC.'inc/search.php');
	global $conf;
	global $ID;
	$dir = $conf['datadir'];
	$ns='templates';
	//$ns  = cleanID('templates');
	//if(empty($ns)){
	//$ns = dirname(str_replace(':','/',$ID));
	//if($ns == '.') $ns ='';
	//}
	$ns  = utf8_encodeFN(str_replace(':','/',$ns));
	//print p_locale_xhtml('index');
 
	$data = array();
	search($data,$conf['datadir'],'search_index',array('ns' => $ns));
	$templateids = array();
	foreach ($data as $item) {
		if (substr($item[id],0,10) == 'templates:') $templateids[] = $item[id];
	}
	return $templateids;	
}
 
function cf_showTemplateList(){
	$templateids = cf_getTemplateIds();
	if (count($templateids) > 0) {
		echo '<input class="button" type="button" value="use template" onClick="document.getElementById(\'wikitext\').value=document.getElementById(\'templatechoice\').value;"></input>';
		echo '<select class="edit" id="templatechoice">';
		foreach ($templateids as $templateid) {
			$raw = rawWiki($templateid);
			$raw = str_replace('"','&quot;',$raw);
			echo "<option value=\"$raw\">".str_replace("templates:","",$templateid)."</option>";
		}
		echo "</select>";
	}
}
?>

Source of copyFrom.css

div.copyFrom {
	text-align:center;
	margin-top: 15px;
}

Usage

  • Copy the files to your /lib/tpl/[whatever] directory.
  • Somewhere in your main.php, include the following lines:
//include copyFrom code
include(dirname(__FILE__).'/copyFrom.php');
  • add a link to the stylesheet:
<link rel="stylesheet" media="screen" type="text/css" href="<?php echo DOKU_TPL;?>copyFrom.css" />
  • and add the function where you want it in your template (I put mine after the wikipage stop line)
	<div class="copyFrom">
	<!-- copyFrom start -->
	<?php html_copyFrom();?>
	<!-- copyFrom stop -->
	</div>
  • create your templates as pages in the templates namespace, and each time you edit a page, you will see a dropdown list of potential templates to use. Once you click on use template, the text in the edit box will be replaced by the text from the chosen template.

Cons

The function actually loads all the text from all of your templates when you begin to edit a document. Ajax would improve this a lot, but I don't have the time for that right now.

 
wiki/discussion/wikitemplates.txt · Last modified: 2006/03/20 10:51 by 195.10.45.152
 
Imprint Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsTranslate