Difference between revisions of "Smarty 4"

From TMM Wiki
Jump to navigationJump to search
 
(23 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
These changes will be required within any implemented smarty code when upgrading to Smarty 4.
 
These changes will be required within any implemented smarty code when upgrading to Smarty 4.
  
Associative array keys are not automatically assigned as variables and logic like this will no longer work with Smarty4:
+
<font color="red">EMPTY TEMPLATES</font> Any template that is empty will cause an error when loaded and break the page. 
<font color="red"><pre>
+
 
 +
 
 +
<font color="red">Associative array keys</font> are not automatically assigned as variables and logic like this will no longer work with Smarty4:
 +
<pre>
 
$smarty->assign($condition_data);
 
$smarty->assign($condition_data);
</pre></font>
+
</pre>
 
need to replace with:
 
need to replace with:
 
<pre>
 
<pre>
Line 12: Line 15:
 
}
 
}
 
}
 
}
 +
</pre>
 +
 +
 +
<font color="red">{html_radios}</font> function works for smarty2, however it does not work too well in Smarty4. 
 +
For example:</br>
 +
[[File:Html_radios.png|border|500px]]</br>
 +
 +
NATS sets the description inside of siteinfo. This displays fine in smarty2 but it will not work too well in smarty4
 +
 +
 +
<font color="red">All the math might not work in the template like:</font>
 +
 +
<pre>{$total_profit/100|currency_format:2}</pre>
 +
</br>
 +
this math logic above will need to replaced with a math equation similar to this:
 +
 +
<pre>
 +
{math equation="a/b" a=$total_profit b=100 assign="total_profit_final"}
 +
{$total_profit_final|currency_format:2}</pre>
 +
 +
 +
In order for some variables to work with other sub templates, you will need to assign it as a <font color="red">smarty variable:</font>
 +
 +
<pre>
 +
{assign var="folder" value="messages" scope="smarty"}
 +
</pre>
 +
 +
 +
<font color="red">{include_php} is deprecated</font> from Smarty, use registered plugins to properly insulate presentation from the application code.
 +
 +
Creating Custom Smarty functions
 +
<pre>
 +
In /nats/includes/smarty/plugins directory, create your custom function like:
 +
 +
function.xxx.php    // replace  xxx with your function name
 +
</pre>
 +
The contents of the function will look like this:
 +
<pre>
 +
<?php
 +
function smarty_function_xxx (array $params, Smarty_Internal_Template $template)  // replace xxx with your custom function name
 +
{
 +
  $a = "test";
 +
  return $a;
 +
}
 +
?>
 +
</pre>
 +
In the template, you can call the function with this syntax:
 +
<pre>
 +
 +
{xxx}  // this will display the word test
 +
 +
</pre>
 +
 +
<pre>
 +
Smarty2 function plugin example :
 +
function smarty_function_xxx($params, &$smarty) {}
 +
 +
Smarty4:
 +
function smarty_function_xxx($params, Smarty_Internal_Template $template) {}
 
</pre>
 
</pre>

Latest revision as of 15:29, 3 January 2023

These changes will be required within any implemented smarty code when upgrading to Smarty 4.

EMPTY TEMPLATES Any template that is empty will cause an error when loaded and break the page.


Associative array keys are not automatically assigned as variables and logic like this will no longer work with Smarty4:

$smarty->assign($condition_data);

need to replace with:

if(is_array($condition_data)) {
foreach($condition_data as $key => $value) {
			$smarty->assign($key, $value); //assign the associative array keys as variables in smarty, except periods
		}
	}


{html_radios} function works for smarty2, however it does not work too well in Smarty4. For example:
Html radios.png

NATS sets the description inside of siteinfo. This displays fine in smarty2 but it will not work too well in smarty4


All the math might not work in the template like:

{$total_profit/100|currency_format:2}


this math logic above will need to replaced with a math equation similar to this:

{math equation="a/b" a=$total_profit b=100 assign="total_profit_final"}
		{$total_profit_final|currency_format:2}


In order for some variables to work with other sub templates, you will need to assign it as a smarty variable:

{assign var="folder" value="messages" scope="smarty"}


{include_php} is deprecated from Smarty, use registered plugins to properly insulate presentation from the application code.

Creating Custom Smarty functions

In /nats/includes/smarty/plugins directory, create your custom function like:

function.xxx.php    // replace  xxx with your function name

The contents of the function will look like this:

<?php
function smarty_function_xxx (array $params, Smarty_Internal_Template $template)   // replace xxx with your custom function name
{
  $a = "test";
  return $a;
}
?>

In the template, you can call the function with this syntax:


{xxx}  // this will display the word test

Smarty2 function plugin example :
function smarty_function_xxx($params, &$smarty) {}

Smarty4:
function smarty_function_xxx($params, Smarty_Internal_Template $template) {}