{{tpl:tag}}
This kind of tag returns a string.
==== Declaration statement ====
The declaration is made with the ''addValue()'' function :
$core->tpl->addValue('CoreVersion',array('tplMyMoreTpl','CoreVersion'));
The first parameter is the tpl tag name. Here, its name is ''CoreVersion''.
The second parameter indicates a class, and a function belonging to this class. Here, the ''CoreVersion'' function of the ''tplMyMoreTpl'' class will be called.
==== Class and function ====
class tplMyMoreTpl
{
public static function CoreVersion($attr)
{
$f = $GLOBALS['core']->tpl->getFilters($attr);
return
'getVersion()').'; ?>';
}
}
This function displays Dotclear's version number. The first line of the function allows us to use [[2.0:resources:themes:tags:common-tag-filters|template tags attributes]].
They are not always necessary so the function could be simplified :
class tplMyMoreTpl
{
public static function CoreVersion()
{
return
'getVersion(); ?>';
}
}
==== Complete code ====
$core->tpl->addValue('CoreVersion',array('tplMyMoreTpl','CoreVersion'));
class tplMyMoreTpl
{
public static function CoreVersion($attr)
{
$f = $GLOBALS['core']->tpl->getFilters($attr);
return
'getVersion()').'; ?>';
}
}
...
This is a **block** tag. The difference between **value** tags and **block** tags lies in their syntax (
$core->tpl->addBlock('EntryIfPosition',array('tplMyMoreTpl','EntryIfPosition'));
The first parameter is the tpl tag name. Here, its name is ''EntryIfPosition''.
The second parameter must be a class and a function belonging to this class. Here, the function ''EntryIfPosition'' of the ''tplMyMoreTpl'' class will be called.
==== Class and function ====
class tplMyMoreTpl
{
public static function EntryIfPosition($attr,$content)
{
$is = isset($attr['is']) ? trim($attr['is']) : '';
$expr = self::testInExpr($is,'$idx');
return
'posts->index()+1; if ('.$expr.'): ?>'.
$content.
'';
}
}
This function checks a post's position compared to the "is" argument that contains accepted position, separed by comas. The function takes two parameters :
* **$attr** which is an array. This way, the parameter "is" of this tag ''
$core->tpl->addBlock('EntryIfPosition',array('tplMoreTpl','EntryIfPosition'));
class tplMoreTpl
{
public static function EntryIfPosition($attr,$content)
{
$is = isset($attr['is']) ? trim($attr['is']) : '';
$expr = self::testInExpr($is,'$idx');
return
'posts->index()+1; if ('.$expr.'): ?>'.
$content.
'';
}
}
===== Demonstration of the template cache influence =====
The following code uses mt_rand(1, 1000) in order to generate a random number between 1 and 1000. In the first function ''Cache()'', the number is generated then directly returned by the function and written as such in the cache. In the second function ''NoCache()'', the number is not stored in the cache : the function is evaluated for every display, thus the number will be different every time.
$core->tpl->addValue('MTRandCache',array('tplMyTest','Cache'));
$core->tpl->addValue('MTRandNoCache',array('tplMyTest','NoCache'));
class tplMyTest
{
public static function Cache()
{
$mt_rand = mt_rand(1, 1000);
return
'';
}
public static function NoCache()
{
return
'';
}
}
{{tpl:MTRandCache}} = {{tpl:MTRandNoCache}}
Forcing the refresh of the page (with ''Ctrl'' + ''F5'' or ''Ctrl'' + ''R'') will change the second number but not the first one.
//(Examples are from the plugin myMoreTpl by Kozlika) (//Instruction blocks// written by Tomtom33).//