Primary use of PHP template Smarty and experience sharing

  • 2020-06-15 07:59:10
  • OfStack

As to how configuration, here need not say more, on the net 1 heap.
1. assign and display methods are the main methods to be used.
2. Basically, the knowledge to be used on the page is if and foreach and section.
Basically, the hardest ones are foreach and section.
4. When I was a beginner, I always couldn't loop out the data I wanted. Because it's a little bit different from php, java, foreach, for. It's hard to catch his active path, and it's hard to debug his data.
5. So here, I'm going to put a little mantra:
If the data looks like this:


array(0=>array('id'=>'1111','content'=>'2222')); then foreach cycle 
{{foreach from=$exam item=item key=k}}
{{$item.content}}
{{/foreach}}

That's fine, but if it's array('id'= > '1111','content'= > '2222') so, you can't loop like this, you need to:

{{foreach from=$exam item=item key=k}}
{{if $k eq 'content'}}
         {{$item}}
{{/if}}
{{/foreach}}

If you don't know what eq means, you can search it on the Internet. I'll list this one a little bit below:
eq are equal,
ne and neq are not equal,
gt is greater than,
lt less than,
gte, ge greater than or equal to,
lte, le less than or equal to,
not not, mod modulus.
Whether is [not] div by is divisible by a number,
Whether is [not]even is even,
$a is [not] by es663en ($a / $b) % 2 == 0
Is is [not] odd surprising

6. In the template of smarty, if you want to use php code, like this:
{{php}}$a = "gayayang"; echo $a;{{/php}}
This allows you to use the php code within the template.

7. Refer to files in smarty:
You can use the above method: {{php} include "config. php"; {{/ php}}
Or professional 1 point: {{include file=" config. php"}}
Both methods can be implemented. The following is more professional point, recommend.

8. Here's how: $smarty- > fetch("mytemplate.html");
Both fetch and display are used for rendering templates.fetch can assign the entire rendered template as a string to a variable that can be used to generate static pages

Now that you know how this works, he can return the contents of the file to a variable, and that's pretty useful. Ha ha
Using the fetch function, you can assign the output value of html to a variable. You can output some data in the variable and then output it.
The display method in smarty, which is actually called by fetch, just displays it directly, while fetch is not shown by default and returns to a variable.

That's about it. I'll write it when I add it later. Ha ha


Related articles: