William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

Smarty preg_match?

Smarty preg_match?

Smarty is a view layer libray, not good at string process. However, when Smarty templates become complex, such case is inevitable.

When use smarty to do regular express, smarty transfer the task to PHP, like this:

{if preg_match("/\.pdf$/", $v)}
<a href="{$m.Path}" title="{$v|escape:'html'}" target="_blank">
 {$v|escape:'html'}
</a>
{else}
<a href="{$m.Path|escape:'html'}" title="{$m.Path}" target="_blank" >
 <img src="{$m.Path}" style="max-height:50px;max-width:110px;" />
</a>
{/if}

In latest Smarty version 3.0.4, it works, and the result is correct. However, for performance, leave the string process to PHP as much as possible.

Advertisements

One response to “Smarty preg_match?

  1. Damiano Venturin (@damko) 01/17/2012 at 5:03 am

    I think it’s better to use a plugin instead of {php} inside the template. You can find the plugin I wrote here http://www.venturin.net/2012/01/17/smarty-v-3-preg_match-plugin/

    Ciao!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: