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

Regular Expressions within the templates

 
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 -> Tips and Tricks
View previous topic :: View next topic  
Author Message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Wed Jul 07, 2010 11:59 am    Post subject: Regular Expressions within the templates Reply with quote

Say, I have a word, "batch-scripts", and I want to print it as "Batch Scripts". I would like regular expressions replace a dash into space, and apply ucwords() behaviour.

It is pretty simple. In the template file, write:
Code:
{'/[^a-z]+/i'|preg_replace:' ':$word|ucwords}


Let me explain you. You do not modify $word here. In fact, '/[^a-z]+/i' pattern. The reason is that the thing you will modify with a pipe ( | ) will be the very first parameter of the modifier.

In this case, the $word is the third parameter, not the first.
Isn't it interesting to work with the modifiers?
Back to top
View user's profile Send private message Visit poster's website
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Wed Jul 07, 2010 12:04 pm    Post subject: Use this within the php codes only Reply with quote

If you think, it should be done within the php code, you are right. I just wanted to show the hidden power of the template tags.

If you need multiple themes, just format the $word variable within the php code only, and help a designer have better life.

Code:
$word = 'batch-scripts'; # some dynamic things here

$word = preg_replace('/[^a-z]+/i', ' ', $word);
$word = ucwords($word);
$smarty->assign('word', $word);


So, your template will shorten to:
Code:
{$word}
only instead of
Code:
{'/[^a-z]+/i'|preg_replace:' ':$word|ucwords}

If you are developing multiple themes, you may consider formatting the variable within the php. If it is a case-based or template based, you can do it within the template. For example, a theme may print the variable in lower case, and other may print it with capitalized, blah blah ...

The choice is yours. I consider both of them as good practice in terms of Smarty.
Back to top
View user's profile Send private message Visit poster's website
ChrisFah
Smarty Pro


Joined: 02 Nov 2009
Posts: 159
Location: Traun, Austria

PostPosted: Wed Jul 07, 2010 4:20 pm    Post subject: Reply with quote

Hi,

thinking about work of template designer, you are right. Your way with modifiers is the shorter code.
But what about the php overhead when using a modifier in smarty variables? Thinking about short execution time and less code overhead, a pure php solution would be faster.

Greetings, Chris
Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Wed Jul 07, 2010 4:32 pm    Post subject: Use a file full with functions Reply with quote

Yes - every call to different modifiers or any other plugins will impact the performance. Access to every single extra fill will slow down.

One tweak would be to include a single big function function file, instead of loading each plugins in by demand. By this, you can avoid the need of real Smarty plugins. You can use any simple function as a modifier - without having to follow the rules of modifiers naming and file system.

Everything should be less:
codes, scripts, execution time and and loading time.


Last edited by bimal on Wed Jul 07, 2010 4:36 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


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

PostPosted: Wed Jul 07, 2010 4:35 pm    Post subject: Reply with quote

Pure PHP may be marginally faster (Smarty tpls are compiled to PHP), but you lose the utility of Smarty (simple syntax, PHP separation, etc.)

You could also argue that writing in C executes faster than writing in PHP, but you lose the utility of PHP.

FYI you can use PHP functions directly as Smarty modifiers.
Back to top
View user's profile Send private message Visit poster's website
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 -> Tips and Tricks 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