简体   繁体   中英

Access value of PHP variable in smarty template

How can the value of a variable that has been set in PHP be accessed in smarty? My example PHP code is:

function searchTwitter() {
    global $smarty;

    $test1 = 'Sometext';
    $smarty->assign("test1", $test1);
    ...
}

My .tpl file will have this section:

test={$test1}

I expect this to display the following in my browser:

test=Sometext

but it doesn't. Could you please advise how I can achieve this? Thanks.

Actually smarty template engine is use for separate php variable and smarty variable. If you want to use php variable in smarty template you need to assign php variable to smarty through $smarty->assign('smarty variable',php variable);

In smarty there is utility is assign which assign smarty variable to another smarty variable at run time.

or {assign varname=varname value=$test1}

now print $varname will print $test1 value

You must use double brackets so try this, it should work:

{{$variable}}

Hope i was helpfull

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