简体   繁体   English

将变量从Smarty分配给PHP

[英]Assign variable from Smarty to PHP

I'm using Smarty for eMail-Templates and I want to have mail subject in one place with the body template. 我将Smarty用于电子邮件模板,并且我希望将邮件主题与正文模板放在一处。

My first try was to set the subject with 我的第一次尝试是将主题设置为

{assign var="subject" value="mySubject"}

and access it with 并使用

$smart->getTamplateVars();

but subject is nowhere to be found. 但无处可寻。

My current working solution is: 我当前的工作解决方案是:

{php}
    global $subject;
    $subject = "My Subject";
{/php}

But using SmartyBC and {php} feels wrong. 但是使用SmartyBC和{php}感觉不对。

Is there a better way to achieve this? 有没有更好的方法来实现这一目标?

You shouldn't assign variables in Smarty and then use it by PHP. 您不应该在Smarty中分配变量,然后在PHP中使用它。 The aproach is to assign variables in your controller and then display it in your view. 方法是在控制器中分配变量,然后在视图中显示它。 You should really read about MVC pattern. 您应该真正了解MVC模式。 Moreover, don't use globals it is really bad solution. 而且,不要使用全局变量,这确实是一个糟糕的解决方案。 {php} tag is very optional and should not be abused. {php}标记是非常可选的,不应滥用。

http://www.smarty.net/sampleapp1 under this link you will find sample app check it and look how it should look like. http://www.smarty.net/sampleapp1在此链接下,您会找到检查示例应用程序,并查看其外观。

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

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