Older Newer
Tue, 18 Jun 2013 22:29:52 . . . . Tim Fountain [Removed spam]


Changes by last author:

Added:
=== Organisation of includes ===

==== Introduction ====

* Some things need to go in the header of the document, if not for correctness, then for clarity

** script-blocks (not allways)

** script-includes

** style-blocks

** style-includes

* The head of the document is in the main Smarty template, but other templates might want to add some includes of their own

* To keep things correct, you should add the include to the main theme template and the code using the include in the subtemplate (where you need it)

** which include is for which template?

** do we want to include files or define things for a subtheme that might not be used on all pages?

** what if a subtemplate can use multiple includes/defines, but chooses just one, when it is parsed?

** locality of change is violated: when a subtemplate is changed, all the templates that use it must be updated too

* To keep things managable you want to include the file right there where you need it in the subtemplate

** script and styleblocks may appear in nestings where they're not allowed

** double includes/defines might occure (like when two templates use prototype.js)

==== A solution ====

* A solution for this is the use of a block and an outputfilter

** each subtemplate includes/defines what it needs, when it needs it

** an outputfilter moves all style and script includes and blocks to the document head in the order they appeared (possibly grouping styles and scripts)

** doubles can be removed, so subtemplates need not to bother wether they were included before to include/define a script or style

==== mechanism ====

* Everything in a {head} block is moved to the end of the <head> of the document

** {head} block puts content between markers

** outputfilter gets content between markers and moves it to the end of <head>

=== block ===

file: plugins/block.head.php

<code>

<?php

/**

* Smarty plugin

* @package Smarty

* @subpackage plugins

*/

/**

* Smarty {head} block plugin

*

* Filename: block_head.php<br>

* Type: block<br>

* Name: head<br>

* Date: April 28, 2006<br>

* Purpose: move all content in headblocks to the header of the html document

*

* Examples:<br>

* <pre>

* {head}

* <style type="text/css">

* h1{font-family:fantasy;}

* </style>

* {/head}

* </pre>

* @author Mathias Baert <mathias@motionmill.com>

* @version 0.1

* @param array

* @param string

* @param Smarty

* @param boolean

* @return string

*/

function smarty_block_head($params, $content, &$smarty, &$repeat){

if ( empty($content) ) {

return;

}

return '@@@SMARTY:HEAD:BEGIN@@@'.trim($content).'@@@SMARTY:HEAD:END@@@';

}

?>

</code>

=== filter ===

file: outputfilter.move_to_head.php

<code>

<?php

/**

* outputfilter adds content of {head}-blocks to document<head>

* @param string

* @param Smarty

* @return string

*/

function smarty_outputfilter_move_to_head($tpl_output, &$smarty)

{

$matches = array();

preg_match_all('!@@@SMARTY:HEAD:BEGIN@@@(.*?)@@@SMARTY:HEAD:END@@@!is', $tpl_output, $matches);

$tpl_output = preg_replace("!@@@SMARTY:HEAD:BEGIN@@@(.*?)@@@SMARTY:HEAD:END@@@!is", '', $tpl_output);

return str_replace('</head>', implode("\n", array_unique($matches[1]))."\n".'</head>', $tpl_output);

}

?>

</code>

=== Usage ===

<code>

$template = new Smarty();

// do stuff

$template->load_filter('output', 'move_to_head');

// do stuff

$template->display($filename);

</code>

==== TODO ====

* The removal of double includes in {head}-blocks is not very inteligent yet: only blocks that are (apart from whitespace at the beginning and end) identical will be detected.

* ex:

<code>

{head}

<link rel="stylesheet" type="text/css" href="global.css" />

{/head}

{head}

<link rel="stylesheet" type="text/css" href="global.css" />

{/head}

{head}

<link rel="stylesheet" type="text/css" href="global.css" />

<style type="text/css">

...

</style>

{/head}

</code>

will become

<code>

<head>

...OLD HEAD...

<link rel="stylesheet" type="text/css" href="global.css" />

<link rel="stylesheet" type="text/css" href="global.css" />

<style type="text/css">

...

</style>

</head>

</code>