Migrating from Smarty to Raintpl3: What You Need to Know
Discover the essential changes and tips for migrating from Smarty to Raintpl3, including syntax adjustments and handling potential bugs.
Key Changes to Note
When you're moving from Smarty to Raintpl3, there are several syntax changes you'll need to make. Let's dive into the main ones:
- If Conditions
- From
{if condition}to{if="condition"}.
- From
- Elseif Conditions
- Change
{elseif condition}to{elseif="condition"}.
- Change
- Including Files
{include "filename.tpl"}becomes{include="filename"}.
- Functions
- Use
{function="Func()"}instead of{Func()}.
- Use
- Functions with Parameters
{Func($param,3)}should be changed to{function="Func($param,3)"}or{$param|Func:3}.
- Foreach Loops
- Update
{foreach $array as $_key => $_val}to{loop="$array" as $_key => $_var}or{loop="$array"}key:{\$key} val:{\$val}{/loop}.
- Update
- Number Formatting
{($variable)|number_format}remains the same but watch out for syntax errors.
- For Loops
- Replace
{for $i=1 to $targetCount}with{loop="range(1,$targetCount)" as $i}.
- Replace
- Assignments
- Convert
{assign var=myObject value=MyClass::myStaticMethod({$variable})}to{$myObject=MyClass::myStaticMethod($variable)}.
- Convert
Additional Notes
- File modification times and translations have specific changes too. For example,
{filemtime("{constant(\"__ROOT__\")}/static/css2/min.css")}should be{function="filemtime(__ROOT__.'/static/css2/min.css')"}.
Regular Expression Replacements
Here's a handy list of regex patterns you can use for a bulk of the syntax changes. This includes adjustments for if conditions, includes, foreach loops, functions, and more.
After making these replacements, you'll need to manually check and adjust complex expressions or functions within your code.
Notable Differences and Bugs
Raintpl3 handles operations differently from Smarty in some cases, especially with the usage of operators like | and :. Also, be aware that Raintpl3 might not support inline variable assignments in includes, which could lead to bugs.
For example, changing variable values before and after includes shows different behaviors between Smarty and Raintpl3.
Found Bugs:
- There's a known issue with using
$keyas a variable in loops, which can cause bugs. (Issue on GitHub)
Migrating from Smarty to Raintpl3 involves more than just syntax changes; it requires a careful review of your templates to ensure everything functions as expected. Happy coding!