Reputation: 3895
I migrated my project from PHP 5.X to 7.0.4. Together with this step, I also had to upgrade the old Smarty 2.6.27 to 3.1.29.
For some reason the rendered templates contain the original smarty sequences, not the expected rendered version.
Example:
index.php:
include_once( "Smarty-3.1.11/libs/SmartyBC.class.php" );
class SmartyExtend extends Smarty {
function __construct() {
parent::__construct();
$this -> compile_dir = "template_c/";
}
}
$smarty = new SmartyExtend();
$smarty -> assign( "greeting", "Hello World" );
$smarty -> display( "my_template.tpl" );
my_template.tpl:
The greeting is: { $greeting }!
Expected result:
The greeting is: Hello World!
Real result:
The greeting is: { $greeting }!
For some reason the delimiters were not handled as delimiters. I deleted the cache folder, used SmartyBC.class.php instead of Smarty.class.php and tried everything else I hoped it could helped. I didn't know whether php7 or smarty3 is culprit. Or, maybe I did something wrong?
How should I ask smarty 3 to understand and recognise the delimiters?
Upvotes: 1
Views: 268
Reputation: 3895
It took a serious amount of time to understand what smarty does. The official smarty delimiters are "{" and "}". However, if the delimiter is surrounded by spaces then they are not handled as delimiters. The reason is to ease writing javascript code into the templates. For example:
<html>
<head>
<script type="text/javascript" >
function init() {
alert( 42 );
}
</script>
</head>
<body>
<h1>{$greeting}</h1>
<p>{ $message }</p>
</body>
</html>
Here, in the javascript code { and } are not smarty delimiters because they are spaces around them. On the other hand, the curly braces inside the H1 tags, well, they are. The ones within the P tag they are not again.
Unfortunately, adding the spaces after the opening and before the closing curly brace, it was part of the code formatting standard in this project. So, what can we do now?
Change the smarty templates. Remove the spaces that are after the { and before the } symbol. This is a nice solution if you have only few opening tags.
In my project there are 7600 opening delimiters so I had to choose another solution. I just simply re-defined the delimiters. I told smarty to use "{ " and " }" delimiters instead of "{" and "}". To achieve this, I simply added the following lines to the end of the SmartyExtend constructor:
$this -> left_delimiter = "{ ";
$this -> right_delimiter = " }";
After this everything started to work fine.
The problem with the previous solution is that if someone, despite the coding standards, forgot to add the spaces inside the delimiters then the concise version won't be recognised as delimiters, only the spaced ones. That is, { $dummy }
will work but {$dummy}
will not. Finding these bugs is very hard if you don't know in advance that this is a potential bug and don't know what to grep for.
Fortunately this behaviour of smarty can be turned off. It is pretty simple, just add the following line to the constructor instead of modifying the delimiters:
class SmartyExtend extends Smarty {
function __construct() {
parent::__construct();
$this -> auto_literal = false; // this is the remedy :)
$this -> compile_dir = "template_c/";
}
}
Fortunately this works fine.
Upvotes: 0