How to use the PHP template Smarty and how it works _ PHP

Source: Internet
Author: User
This article provides a detailed analysis of the initial usage and experiences of the PHP template Smarty. For more information, see As for how to configure it, I don't need to talk about it here. There are a lot of Internet resources.
1. the most important method to use is the assign and display methods.
2. Basically, the knowledge used on the page is the if, foreach, and section.
3. the most difficult part is foreach and section.
4. when you are a beginner, you can't cycle the data you want. It seems that foreach and for are different from those of php and java. It is difficult to grasp his activity path and debug his data.
5. here, I will read a little bit:
If the data is as follows:
The code is as follows:
Array (0 => array ('id' => '000000', 'content' => '000000'); then the foreach loop
{Foreach from = $ exam item = item key = k }}
{{$ Item. content }}
{{/ Foreach }}

But if it is array ('id' => '000000', 'content' => '000000'), it is not possible to loop like this. you need to do this:
The code is as follows:
{Foreach from = $ exam item = item key = k }}
{If $ k eq 'content '}}
{{$ Item }}
{/If }}
{{/ Foreach }}

If you do not know what eq means, you can search for it online. I will list it below:
Equal eq,
Ne, neq are not equal,
Gt>,
Lt is less,
Gte and ge are greater than or equal,
Less than or equal to lte and le,
Not Non. mod modulo.
Is [not] can p by be divisible by a certain number,
Is [not] Whether the even is an even number,
$ A is [not] even by $ B ($ a/$ B) % 2 = 0,
Is [not] is odd?

6. in the smarty template, if you want to use php code, like this:
{Php} $ a = "gayayang"; echo $ a; {/php }}
In this way, php code can be used in the template.

7. reference the file in smarty:
You can use the following methods: {php} include "config. php"; {/php }}
Or professional: {include file = "config. php "}}
Both methods can be implemented. The following are more professional and recommended.

8. there is a method like this: $ smarty-> fetch ("mytemplate.html ");
Both fetch and display are used for rendering templates. fetch can assign a value to a variable in the form of a string for rendering the entire template. it can be used to generate static pages.

Now I know how to use this. he can return the file content to a variable, which is very useful. Haha
Using the fetch function, you can assign a value to the html output to a variable, and output some data in the variable.
In smarty, the display method actually calls fetch, but directly displays it. fetch is not displayed by default and is returned to a variable.

This is basically the case. I will try again later. Haha

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.