Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

block functions without content (XHTML compatible)

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
rainco
Smarty Rookie


Joined: 13 Oct 2003
Posts: 31
Location: Germany

PostPosted: Thu Feb 26, 2004 4:53 pm    Post subject: block functions without content (XHTML compatible) Reply with quote

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
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Fri Feb 27, 2004 7:40 am    Post subject: Reply with quote

You could create your own prefilter (see http://smarty.php.net/manual/en/advanced.features.prefilters.php) which expands {block /} to {block}{/block} automatically during compilation using a regular expression for example.
Back to top
View user's profile Send private message
rainco
Smarty Rookie


Joined: 13 Oct 2003
Posts: 31
Location: Germany

PostPosted: Fri Feb 27, 2004 10:11 am    Post subject: Reply with quote

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
View user's profile Send private message
rainco
Smarty Rookie


Joined: 13 Oct 2003
Posts: 31
Location: Germany

PostPosted: Fri Feb 27, 2004 12:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Feb 27, 2004 4:50 pm    Post subject: Reply with quote

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 Smile 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 Wink
Monte
Back to top
View user's profile Send private message Visit poster's website
rainco
Smarty Rookie


Joined: 13 Oct 2003
Posts: 31
Location: Germany

PostPosted: Sat Feb 28, 2004 12:48 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP