Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use PHP template Smarty

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use the PHP template Smarty, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

1. Basically, the most important methods to use are the assign and display methods.

two。 Basically, the knowledge to be used on the page is if and foreach and section.

3. Basically the hardest ones are foreach and section.

4. When I was a beginner, I couldn't cycle out the data I wanted. Because it still feels different from php, java, ah, these foreach and for. It's hard to catch his activity path, it's hard to debug his data.

5. So here, I put out a little bit of scripture:

If the data is like this:

The copy code is as follows:

Array (0 = > array ('id'= >' 1111); then the foreach loop

{{foreach from=$exam item=item key=k}}

{{$item.content}}

{{/ foreach}}

It will be fine, but if it is array ('id'= >' 1111 correspondence = > '2222'), you can't loop like this, you need to do this:

The copy code is as follows:

{{foreach from=$exam item=item key=k}}

{{if $k eq 'content'}}

{{$item}}

{{/ if}}

{{/ foreach}}

If you don't know what eq means here, you can search it on the Internet. I'll list this a little bit below:

Eq equality

Ne and neq are not equal.

Gt is greater than

Lt is less than

Gte, ge greater than or equal to

Lte, le less than or equal to

Not is not, mod is modeled.

Whether is [not] div by can be divisible by a number.

Whether is [not] even is even

$an is [not] even by $b is ($a / $b)% 2 = = 0

Is is [not] odd strange?

6. If you want to use php code in the template of smarty, so:

{{php}} $a = "gayayang"; echo $a; {{/ php}}

This allows you to use php code in the template.

7. Reference files in smarty:

You can use the above method: {{php}} include "config.php"; {{/ php}}

Or be professional: {{include file= "config.php"}}

Both methods can be implemented. The following is a more professional point, recommended.

8. There is a method like this: $smarty- > fetch ("mytemplate.html")

Both fetch and display are used for rendering templates. Fetch can assign the entire rendered template to a variable in the form of a string, which can be used to generate static pages

Now that you know how to use this, he can return the contents of the file to a variable, which is very useful. He he

Using the fetch function, you can assign the output html to a variable, and you can output some of the data inside, and then output it.

The display method in smarty actually calls fetch, but displays it directly, while fetch does not display it by default and returns it to a variable.

Thank you for reading this article carefully. I hope the article "how to use PHP template Smarty" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report