|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Harald Stowasser Smarty n00b
Joined: 13 Jul 2007 Posts: 2
|
Posted: Fri Jul 13, 2007 2:47 pm Post subject: macro a prefilter for smarty text modules |
|
|
I wrote a little macro-preprocessor. Maybe you can use it to save some typing or to avoid errors.
It would be nice if anybody can review the code. Especially the Regular expressions could take a second glance.
Have fun....
Code: |
<?
/*****************************************************************
* A prefilter for makros in a smarty-template.
* @author = Stowasser Harald, Freinet GmBH, Germany. <stowasser@freinet.de>
* @date =12.07.2007
* @version 0.1
* @param string $tpl_source The template source code.
* @param Smarty &$smarty The Smarty wich has called this filter
*
* With this prefilter You can set macros in a smarty-Template.
* the macros will be replaced before the smarty-compiler does his work
* The Filter can allso handle macros with content.
* examples:
*
// This defines an output for your error
{set_macro error}
<b class="error">{$error}</b>
{/set_macro error}
// so this call:
{if $ding|has_error}{get_macro error}{/if}
// will be replaced to:
{if $ding|has_error}<b class="error">{$error}</b>{/if}
// The examlpe for my lable is a litle bit harder:
// The {$content}-Tag will be replaced with the text between {get_macro label} and {/get_macro label}
{set_macro label}
{if $form_last_element|is_required}
<label class="pflicht" for="{$form_last_element}">{$content}*</label>
{else}
<label class="normal" for="{$form_last_element}">{$content}</label>
{/if}
{/set_macro label}
{assign var='form_last_element' value='adr_forename'}
{get_macro label}Forename:{/get_macro label}
<input type="text" name="{$form_last_element}" value="{$adresse->getForename()}"/>
*/
function smarty_prefilter_macro($tpl_source, &$smarty) {
// Search for a set_macro-tag:
$macroset=array();
$pattern = "/[{] *set_macro .*?[}](.*?)[{] *\/ *set_macro ([\w]*)?.*?[}]/s";
$text=$tpl_source;
preg_match_all($pattern,$tpl_source,$macroset);
// And then delete it from the template:
$text= preg_replace("/[{] *set_macro .*?[}].*?[{] *\/ *set_macro .*?[}]/s","",$tpl_source);
// Now replace the get_macro-tags
if (count($macroset)==3){
$anz=count($macroset[2]);
for ($i=0;$i<$anz;$i++){
// is this a macro with content?
$content=array();
$macrotext=$macroset[1][$i];
$macro='get_macro '.$macroset[2][$i];
$pattern = "/[{] *".$macro.".*?[}](.*?)[{] *\/ *".$macro.".*?[}]/s";
preg_match_all($pattern,$text,$content);
if (count($content)>0){
// then replace the Content
$anz2=count($content[1]);
for ($j=0;$j<$anz2;$j++){
$neutext=preg_replace("/[{] *[$]content.*?[}]/",$content[1][$j],$macrotext);
$text =str_replace( $content[0][$j], $neutext, $text);
}
}
//If there is no closing tag just do a replace.
$text=preg_replace("/[{] *".$macro.".*?[}]/",$macroset[1][$i],$text);
}
}
return $text;
}
?> |
Also aviable at:
http://smarty.incutio.com/?page=macro |
|
Back to top |
|
Harald Stowasser Smarty n00b
Joined: 13 Jul 2007 Posts: 2
|
Posted: Thu Apr 02, 2009 12:18 pm Post subject: |
|
|
Version 0.3 is now online at incutio
Now you can define a File to include the macros. Thats good for a set of standard macros. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Apr 02, 2009 1:41 pm Post subject: |
|
|
There are lots of other ways to handle this too. Example:
Code: | {include file="macros.tpl" name="error} |
Then just have a template full of your macros.
Or, use config files to hold macros.
A prefilter is efficient though, it does the replacement once, at compile time. But if you ever changed a macro, you must remember to recompile. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|