Reputation: 329
I am converting an application from Smarty 3 to Twig 2 and we use this command about 9,000 times throughout this application:
$smarty->assign('name', $var);
I began replacing these lines with $twigvars['name'] = $var;
and then using the standard $twig->display('whatever.tpl', $twigvars);
to render the templates but I am wondering if anyone has come up with a find & replace way to convert those $smarty->assign
lines into what we need to use with Twig as it's becoming a bit of a lot of manual labor to just replace these lines. I imagine there has got to be a smarter way than typing all of that out.
Upvotes: 0
Views: 445
Reputation: 8540
How about doing a search-and-replace using regular expressions in your IDE or code editor? If you go through the search results one by one, you can check that every hit is replaced correctly. Going through 9,000 hits is gonna take some time, but at least you don't have to type everything manually.
Find:
\$smarty->assign\('([^']+)', (\$[a-zA-Z0-9_]+)\);
# Replace:
$twigvars['$1'] = $2;
Then e.g. $smarty->assign('name', $var);
will be replaced with $twigvars['name'] = $var;
.
You may also want to use the following variation if you have used double quotes in some places, e.g. $smarty->assign("name", $var);
.
Find:
\$smarty->assign\("([^"]+)", (\$[a-zA-Z0-9_]+)\);
# Replace:
$twigvars["$1"] = $2;
Then e.g. $smarty->assign("name", $var);
will be replaced with $twigvars["name"] = $var;
.
I'm sure you can come up with all other variations you need by yourself.
This approach might be more effortless.
Search for all occurrences of $smarty->assign
and replace them with e.g. $twigvars->assign
. It can be done quickly without having to go through all hits to see that every hit is replaced correctly. (With regular expressions you'd better be more careful.)
Then create a simple class like this:
<?php
$twigvars = new class {
private $vars = [];
public function assign($name, $value) {
$this->vars[$name] = $value;
}
public function getVars() {
return $this->vars;
}
};
$var1 = 123;
$twigvars->assign('integer', $var1);
$var2 = 'hello world';
$twigvars->assign('string', $var2);
$var3 = new stdClass();
$twigvars->assign('object', $var3);
// Then you can:
$twig->display('whatever.tpl', $twigvars->getVars());
Upvotes: 0
Reputation: 1363
Maybe worth considering to use a use some wrappers, that way you dont really care about what template engine you use, if you need to change again in the future.
pseudo stuff:
class Content {
private $content;
public function set($key, $val){ $this->content[$key] = $val; }
public function get(){ return $this->content; }
}
class Template {
private $handle;
public function __constructor(){ $this->handle = new twig... }
public function render($tempalte, $content){
return $this->handle->display($template, $content);
}
}
then you could do stuff like
$Content->set("var", $var);
echo $Template->render("file.tpl", $Content->get());
That way you could also use find and replace $smarty->assign
to $Content->set
Upvotes: 1