如何使用php代码在tpl文件的协作在smarty


How to use php code in tpl file in collabtive in smarty?

我需要在smarty的tpl文件中使用php代码。我用的是{php} echo "hello"; {/php}但是我需要在php代码中使用一个smarty变量。

例如,我需要使用以下变量{$myprojects[project].ID}在以下php代码索引。TPL文件

{php}
    $qry = "select name from tasklist WHERE project = ".{/php} { {php}$myprojects[project].ID {/php} } {php}." ";
    echo $qry;
{/php}

你有一个$this Smarty对象在每个模板:

$this->get_template_vars('myprojects')

你必须这样写你的代码

{php}
    $var = $this->get_template_vars('myprojects');
    // if it is not an array you can use directly and if it is an array use as below.
        $qry = "select name from tasklist WHERE project = ".$var['key'];
        echo $qry;
    {/php}

关于你的知识和更好的编码帮助,见下面

最好在PHP文件中创建一个类并调用类的对象,然后开发一个函数来获得所需的输出。

    $objMyF = new my_functions();
    $smarty->assign('objMyF',$objMyF);
    //and in your tpl file you can call its functions by
    {$objMyF->function_name($var)}