|
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 |
rainco Smarty Rookie
Joined: 13 Oct 2003 Posts: 31 Location: Germany
|
Posted: Thu Feb 26, 2004 4:53 pm Post subject: block functions without content (XHTML compatible) |
|
|
hi!
i have some block functions. but some of them do not always need content. what do you think about allowing {block /} instead of {block}{/block}?
the block functions code should then also be processed twice, so that it is identically to the long {block}{/block} syntax. |
|
Back to top |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
|
Back to top |
|
rainco Smarty Rookie
Joined: 13 Oct 2003 Posts: 31 Location: Germany
|
Posted: Fri Feb 27, 2004 10:11 am Post subject: |
|
|
thanks andre!
here is the prefilter for everybody who wants to use it:
Code: |
<?php /* $Id$ */
/*
* Smarty plugin
* -------------------------------------------------------------
* File: prefilter.shortblocks.php
* Type: prefilter
* Name: shortblocks
* Purpose: Convert {test param="value" /} to
* {test param="value"}{/test}
* -------------------------------------------------------------
*/
function smarty_prefilter_shortblocks($source, &$smarty)
{
$ldlim = $smarty->left_delimiter;
$rdlim = $smarty->right_delimiter;
$pldlim = preg_quote($ldlim, "/");
$prdlim = preg_quote($rdlim, "/");
return preg_replace("/".$pldlim."(\S*)(\s.*)*\s*\/\s*".$prdlim."/U",
$ldlim."\\1\\2".$rdlim.$ldlim."/\\1".$rdlim,
$source);
}
?>
|
|
|
Back to top |
|
rainco Smarty Rookie
Joined: 13 Oct 2003 Posts: 31 Location: Germany
|
Posted: Fri Feb 27, 2004 12:04 pm Post subject: |
|
|
hmm,
the prefilter does not work if you have a shortblock in a normal block:
{outer}foo{inner /}bar{/outer}
expands to
{outer}foo{inner}{/outer}foo{inner}
but the regex ist already ungreedy through the U modifier. can anybody help me to correct it?
i think i have to ensure that (\S*)(\s.*)* cannot have any smarty delimiters in it, but how can do that?
thanks,
rainer |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Feb 27, 2004 4:50 pm Post subject: |
|
|
rainco wrote: | hmm,
the prefilter does not work if you have a shortblock in a normal block:
{outer}foo{inner /}bar{/outer}
expands to
{outer}foo{inner}{/outer}foo{inner}
but the regex ist already ungreedy through the U modifier. can anybody help me to correct it?
i think i have to ensure that (\S*)(\s.*)* cannot have any smarty delimiters in it, but how can do that?
thanks,
rainer |
Welcome to regular expression hell There is a way to do this, and I'll try to explain...
First of all, if you were looking for a slash at the beginning of the match, it would work. Perl can find "{/" then go forward to the first "}" with ungreedy matching. But since you are wanting to find the slash at the end of the expression, Perl can't handle that. ie, it can't anchor its search to "/}", then go backwards until it encounters the first "{". (Perl has look-behind on fixed-width content, but that won't work for us.)
So to work around this problem, you have to reverse your string first, then use the tools Perl has to do your preg_replace, then swap the string back again.
So your example:
{outer}foo{inner /}bar{/outer}
use strrev() on it:
}retuo/{rab}/ renni{oof}retuo{
Hey cool, now we can look for "}/ renni{" and replace that with "}renni/{}renni{".
I'll take a stab at something for you to start with, this is untested:
$source = strrev($source);
$source = preg_replace("%}/\s*(.*?){%U", "}\\1/{}\\1{", $source);
$source = strrev($source);
You'll have to work your $pldelim and $prdelim into that.
Good luck
Monte |
|
Back to top |
|
rainco Smarty Rookie
Joined: 13 Oct 2003 Posts: 31 Location: Germany
|
Posted: Sat Feb 28, 2004 12:48 pm Post subject: |
|
|
thanks monte! now it works i think (hope so *g*)!
here it is for everybody free to use:
Code: |
<?php /* $Id$ */
/*
* Smarty plugin
* -------------------------------------------------------------
* File: prefilter.shortblocks.php
* Type: prefilter
* Name: shortblocks
* Purpose: Convert {test param="value" /} to
* {test param="value"}{/test}
* -------------------------------------------------------------
*/
function smarty_prefilter_shortblocks($source, &$smarty)
{
$l = $smarty->left_delimiter;
$r = $smarty->right_delimiter;
$pl = preg_quote($l, "%");
$pr = preg_quote($r, "%");
return strrev(preg_replace("%".$pr."\s*/(\s*?)(.*)\s*(\S*)".$pl."%U",
$r."\\3/{}\\2 \\3".$l,
strrev($source)));
}
?>
|
|
|
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
|