How to add PHP code to .tpl file [duplicate]

限于喜欢 提交于 2019-11-28 04:25:18

问题


I need to display some external data from php file to .tpl file. For this I want to include php file to .tpl file. I have tried folllowing code to display php file content to tpl.

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

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


回答1:


{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}



回答2:


You shouldn't add PHP code to the template. It will make whole idea of templates spoiled.

You have to add PHP code to controller, not template.




回答3:


There is a best practise guide on smarty homepage. #1 is Do not embed PHP!

http://www.smarty.net/best_practices

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

But notice:

{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.

So best way is to write a smarty plugin as explained by rodneyrehm



来源:https://stackoverflow.com/questions/9601547/how-to-add-php-code-to-tpl-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!