Using PHP Code in Smarty Tpl File

How to include php code in .tpl file

You should not inject php code in any kind of template (not only Smarty). Load your data and perform your logic in php and render in the templating. engine. There is no need for template functions or to include php in your case.

PHP file

// Initiate smarty
$smarty = new Smarty ...;
...

// Somehow load your data from file
$itemsFromFile = somehow_load_data_from_file( ... );
...

// PAss your data to Smarty
$smarty->assign('items', $itemsFromFile);
...

// Render your template
$smarty->display( ... );

TPL file

<table>
<tr>
<th>Name</th>
<th>Lastname</th>
<th>Phone</th>
</tr>

{foreach $items as $key => $item}
{if $key % 3 == 0}
<tr>
{/if}
<td>$item</td>
{if $key % 3 == 2}
</tr>
{/if}
{/foreach}
</table>

Use the advantages of the templating engine. You can use modulus of three instead of counting to two and then reseting to zero.

Sources:

  • https://www.smarty.net/docsv2/en/language.function.if.tpl
  • https://www.smarty.net/docsv2/en/language.function.foreach.tpl
  • Passing variable from PHP to Smarty

Assign php code to smarty template engine

You can't do that in Smarty. Also running php code stored in the database sounds like a terrible idea. But if for some reason you have to go on with this nonsense (and considering that you can't use eval), you can try this:

  1. Read the php code from the database.
  2. Save to a temporal php file
  3. Turn on output buffering with ob_start()
  4. include the file you have created
  5. assign the output to a variable with ob_get_clean()
  6. assign the variable to the template

But if I was you, I would try to do the project in another way.

How to add PHP code to .tpl file

{php} has been deprecated. Have a look at Extending Smarty With Plugins.

put the follwing in …/plugins/function.yourplugin.php:

<?php
function smarty_function_yourplugin(array $params, Smarty_Template_Instance) {
include 'your_other_file.php';
}

and use in your template:

{yourplugin}

How to run php code in smarty template file?

Are you using Smarty3? Have a look here.

{php} tag is deprecated, and from version 3.1 it's only available using SmartyBC class instead of Smarty class. They are just the same, but if you instance a new SmartyBC() object you will have a backward-compatible Smarty object supporting {php} tags.

Using PHP inside Smarty template files

To expand the comments already given into a full answer, the problem with embedding arbitrary PHP code is that it breaks down the separation between PHP and Smarty.

Since Smarty compiles directly to PHP, everything you write in Smarty can be written in pure PHP, and may or may not end up just as readable, e.g.

  • <?= $foo ?> for {$foo} is fine
  • <?php if ( $expr ): ?> Hello <?php endif; ?> for {if $expr} Hello {/if} is not too bad either
  • but <?= htmlspecialchars(strtoupper($foo ?: 'Hello')); ?> for {$foo|default:'Hello'|upper|escape:html} is a bit harder on the eye

The main advantages I see of using Smarty (all of which are basically voided if you use {php}):

  • Obvious separation - it is always clear that a given file is considered a Template, because it's in Smarty. Conversely, it is always clear that output directly within a PHP function is wrong, because the result should be being passed to the template.
  • Forced separation - application logic such as database connectivity or input validation cannot "leak" into your View layer if you don't allow those functions to be called from Smarty templates
  • Security - you can give non-trusted front-end developers / designers access to Smarty templates and tightly control the data and functionality they have access to.

The only benefit you'd retain if you mixed Smarty and PHP would be those parts of Smarty syntax which are more readable than their PHP equivalent, like the modifier example I showed above.



Related Topics



Leave a reply



Submit