简体   繁体   中英

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.

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.

Is there a better way to achieve this?

You shouldn't assign variables in Smarty and then use it by PHP. The aproach is to assign variables in your controller and then display it in your view. You should really read about MVC pattern. Moreover, don't use globals it is really bad solution. {php} tag is very optional and should not be abused.

http://www.smarty.net/sampleapp1 under this link you will find sample app check it and look how it should look like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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