How to output variables using php labels in smarty

Source: Internet
Author: User
How to output the variable index using the php tag in smarty. phpPHPcode $ smarty = newSmarty; $ smarty-& gt; allow_php_tag = true; $ smarty-& gt; assign (& quot; a & quot;, & quot; aaaaaaaaaaaa & quot ;) how to output variables using php labels in smarty
Index. php
PHP code
  $smarty = new Smarty;$smarty->allow_php_tag=true;$smarty->assign("a", "aaaaaaaaaaaaaa");$smarty->display('index.tpl');


Index. tpl
{Php}
Echo $;
{/Php}

Can I use the php tag to output the variable $?

------ Solution --------------------
Assume that the specified smarty delimiter is {}, and then {$}
------ Solution --------------------
Assume that your delimiter is:
In SMARTY, output the PHP variable directly in the following way. However, you must enable allow_php_tag = true in the program;


Echo $;



------ Solution --------------------
Yes. the syntax in the php tag is the same as in the php file.
------ Solution --------------------
{$ }.
------ Solution --------------------
Assume that your separator is: {} directly {$ }.
------ Solution --------------------
The separator is {}, which can be directly {$ }.
------ Solution --------------------
That is, adding an echo is simply an extra step.
------ Solution --------------------
{$}
------ Solution --------------------
{Php}
Echo $ aaaaaaaaaaaa;
{/Php}

------ Solution --------------------
Assign is a variable. just enclose the variables in assign with delimiters on the page.
------ Solution --------------------
In Smarty2, php labels are opened by default, that is, the template is identified by default, and Smarty3 needs to be set. It can also be seen that using php labels in templates is not a recommended practice. LZ can implement the following methods.
PHP code
// Write global $ var in the PHP file like this; // declare the global variable... $ var = 'Hi Smarty '; // value assignment. you do not need to use the assign method of smarty to obtain <{php}> echo $ GLOBALS ['var'] in this way. <{/php}>
------ Solution --------------------
Why use the php label? {variable} is enough. you can use foreach section for loop.
------ Solution --------------------
Direct
{$ A} is enough. if you want to process the data, you can refer to the manual.
------ Solution --------------------
Yes, but you must define the delimiters,
$ Smarty-> left_delimiter = "{php}"; // specify the left delimiter
$ Smarty-> right_delimiter = "/{php}>"; // specify the left delimiter
The echo in the middle is not needed. it is {php} $ a {/php} directly}

------ Solution --------------------
[Php]
Echo "this is the php code ";
[/Php]

You can directly write php code in the template.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.