简体   繁体   English

在Smarty中如何在tpl文件中使用php代码?

[英]How to use php code in tpl file in collabtive in smarty?

I need to use php code in tpl file in smarty. 我需要在smarty的tpl文件中使用php代码。 I used {php} echo "hello"; {/php} 我用{php} echo "hello"; {/php} {php} echo "hello"; {/php} But I need to use a smarty variable in php code. {php} echo "hello"; {/php}但是我需要在php代码中使用一个聪明的变量。

For example I need to use following variable {$myprojects[project].ID} in following php code in index.tpl file 例如,我需要在index.tpl文件中的以下php代码中使用以下变量{$myprojects[project].ID}

{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')

You have to write your code like this 您必须像这样编写代码

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

for your knowledge and better coding help see below 就您的知识和更好的编码帮助而言,请参见下文

it is better you can create a class and call an object of class in you php file and develop a function to get desired output. 最好您可以在您的php文件中创建一个类并调用该类的对象,并开发一个函数以获取所需的输出。

    $objMyF = new my_functions();
    $smarty->assign('objMyF',$objMyF);

    //and in your tpl file you can call its functions by
    {$objMyF->function_name($var)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM