Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Some useful regexp replacements while upgrading from Smarty2

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
ikund
Smarty Rookie


Joined: 22 Nov 2011
Posts: 9

PostPosted: Wed Mar 14, 2012 3:52 pm    Post subject: Some useful regexp replacements while upgrading from Smarty2 Reply with quote

I found the upgrade from Smarty 2 to be quite difficult, because the system I was upgrading (a commercial CMS) had hundreds of templates and dozens of different clients/projects were using it, all with slight modifications. Most templates had some kind of incompatibility with Smarty 3 (mostly due to the more strict nature of the new version).

Anyhow, luckily most of them could be removed with some help from regular expressions, which could be added to the upgrade scripts.

The following expressions fix the most common problems I had with the templates. Hope that it is of use to anyone some day.

Code:


$aSearch = array(
   "/(\{include.*file=)(db:.*)([\s\}])/U",                           // converts {include file=db:Template} to {include file="db:Template"}
   "/\{defun\sname=\"(.*)\"\s(.*)\}(.*\{fun.*\}.*)\{\/defun\}/imxsU",// converts old "defun" definitions to new "{function}" calls
   "/\{fun\sname=\"(.*)\"(.*)\}/imxsU",                              // converts old "{fun}" function calls to new type
   '/\{\$(.*\|\w+:)([^\(\)\}\{\'\"]+[\*\/\+\-]\d{1,3})(.*)\}/iU',    // converts {$var|indent:2*2} to {$var|indent:(2*2)}}
   '/\{\$(.*)\|escape ([\'\"a-z]+)\}/iU',                           // converts {$data.var|escape html} to {$data.var|escape:html}
   '/\{(assign .*)value=(`.*`)(.*)\}/iU',                           // converts {assign var=t value=`$data.item`} into {assign var=t value="`$data.item`"}
);

$aReplace = array(
   "$1\"$2\"$3",
   sprintf('{function name="$1"}$3{/function}%s{$1 $2}', PHP_EOL),
   '{$1$2}',
   '{$$1($2)$3}',
   '{$$1|escape:$2}',
   '{$1value="$2"$3}',
);

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 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