How to add PHP code to a .tpl file

I need to map some external data from a php file to a .tpl file. For this, I want to include a php file in a .tpl file. I tried using the following code to display the contents of a php file in tpl.

{php} include('custom_code.php'); {/php}

but the output of the page was include('custom_code.php');

+5
source share
3 answers

{php}outdated. Check out the Smarty Extension with plugins .

enter value 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}
+4
source

The smarty homepage has a guide to best practices. # 1 - do not embed PHP!

http://www.smarty.net/best_practices

Try the following: {include_php file="/path/to/somefile.php"}

But note:

{include_php} is deprecated from Smarty, use registered plugins 
to properly insulate presentation from the application code. 
As of Smarty 3.1 the {include_php} tags are only available 
from SmartyBC.

This is the best way to write a smarty plugin, as Rodneyrehm explained

+1
source

PHP- . .

PHP- , .

+1

All Articles