Translations of this page?:

Include a wiki-page in your template

I needed users to be able to edit parts of the main template (e.g. menu's etc), so I wanted to include wiki pages in the template.

I created the following very simple script that I place in a file called `template_includer.php` in the directory of my template.

My template

This is a snippet of my main.php file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
/**
 * DokuWiki Default Template
 *
 * This is the template you need to change for the overall look
 * of DokuWiki.
 *
 * You should leave the doctype at the very top - It should
 * always be the very first line of a document.
 *
 * @link   http://wiki.splitbrain.org/wiki:tpl:templates
 * @author Andreas Gohr <andi@splitbrain.org>
 */
 
 require_once('template_includer.php');
?>
 
//--- .. stuff .. //-->
 
  <div id="Menu">
    <?php echo getTemplateInclude('Templates:Menu'); ?>
  </div>
 
//--- .. stuff .. //-->
 
 
<?php tpl_indexerWebBug()?>
</body>
</html>


My included wiki page (Template:Menu)

This is how the Template:Menu looks like on my site:

  * [[:startHome]]
  * [[:About]]
  * [[:Projects]]
 
  * [[:Links]]
 
  * [[this>doku.php?do=logoutLogout]] (AUTHED_USERS)

Note that the (AUTHED_USERS) addition is not visible in the output, but makes that line invisible to users that arent logged in.


template_includer.php

<?php
  /***
   * Include a wiki page into your website template (so users can edit parts of your template on the wiki).
   *
   * Example:
   *
   *  <?php require_once('template_includer.php'); ?>
   *  <?php echo getTemplateInclude("Templates:MainMenu"); ?>
   *
   * @param sPageName The full-id of the page in the format `namespace:pageid` (e.g. 'menu' or 'templates:menu')
   * @param sRevision The revision we want (optional)
   * @param aReplaceArray Optionally you can start with an array of things in the output you would like to have replaced 
   *
   * @link   http://wiki.splitbrain.org/wiki:tips:include_wiki-page_in_template
   * @author David Kerstens <david@endora.nl>
   */
  function getTemplateInclude( $sPageName, $sRevision = '', $aReplaceArray = array()) {
	  	$sData = "";
 
		# Using standard wiki functions we'll get the cached parsed xhtml output of the
		# requested page (and of the given (optional) revision)
		foreach (split("\n", p_cached_xhtml( wikiFN($sPageName,$sRevision) )) as $sLine) 
		    # We skip lines with (AUTHED_USERS) in them if the users arent authed
			if ( strpos($sLine, "(AUTHED_USERS)")==FALSE )
				$sData .= $sLine;
			elseif( IsSet($_SERVER["REMOTE_USER"]) ) {
				$sData .= $sLine;
				$aReplaceArray["(AUTHED_USERS)"] = '';
			}
		# Return the output, but replace the things we dont want to show:
		return strtr( $sData, $aReplaceArray );
	}
?>

Questions

I love this template includer function. It opens up the larger opportunity to add a (CMV) view mechanism to the whole template structure. But at the moment, I have a simple question: why does the function render wiki headlines like ===headline=== as

<a name="headline">

as opposed to the usual

<a href="headline">

Thanks in advance to anyone who can help me out!

The name attribute let's urls like http://site.com/page.html#section scroll the page to where <a href=“section” /> was written. This is particularly handy to link titles in the TOC to the relevant section. — Grahack 2008/06/16 16:51
That's true and thanks for your reply Chris. What I should have made more clear is that the link is created with a name attribute and there is no href attribute, meaning that if you include an article in a page, you cannot click on the title to get to that article's page.
 
wiki/tips/include_wiki-page_in_template.txt · Last modified: 2008/06/18 19:59 by 71.15.83.168
 
Imprint Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki
WikiForumIRCBugsTranslate