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

What are the common simple applications of Smarty templates

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces you what are the common simple applications of the Smarty template, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The first step is to include the class smarty:

Include_once'.. / libs/Smarty.class.php'

Then create a Smarty object:

$smarty = new Smarty

You can customize the start and Terminator of Smarty, which defaults to {}

$smarty- > left_delimiter ='>'; / / the right symbol is > >

The most important method seems to be assign, such as:

$smarty- > assign ('test',$te); / / pay the value of $test to test and display it in the template page tpl with {$test}

Another example is $arr=array (1, 2, 5, 3); the assignment is still like this:

$smarty- > assign ('arr',$arr)

However, the use of foreach or section,foreach when displaying the template page is as follows:

{foreach item=item from=$arr key=ke name=foe} $item {/ foreach} / $item here is equivalent to $arr [$ke], foreach serialization {$smarty.foreach.foe.iteration}

The usage of section is as follows:

{section name='test' loop=$arr} {$smarty.section.name.iteration} / / serializes the output, with the sequence number starting at 1 and index starting at 0 {/ section}

Don't forget the last and most important step:

$smarty- > display ('test.tpl')

Here's how some common things are displayed on the template.

1. Connect operation:

My name is {$str1 | cat: "Li Bai"}; / / the output is: my name is $str1 Li Bai

two。 Current date:

{$str2 | rdate_format: "Y% color m% color d%"} / / output formatted $str2 date, such as 0000-00-00

3. Indent:

{$str3 | indent:8: "*"} / / $str3 is preceded by 8 indents * spaces are indented by default

4. Case:

{$str4 | lower} / / $str4 in lowercase {$str4 | upper} / / $str4 in uppercase

Filter:

{$url | escape: "url"} / / A pair of special characters related to $url are replaced / / tr background alternating colors are # EBEBEB,#ACABAB respectively

Match replacement:

{$str | regex_replace: "~ [0-9] ~": "asd"} / / if $str matches [0-9] output asd

Replace

{$str | replace: "net": "com"} / / replace all net in $str with com

Include the header template file:

{include file= "top.tpl"}

Call the function in time.inc.php:

{insert name= "getCurrentTime" assign= "current_time" script= "time.inc.php"} current time is {$current_time}; {/ insert}

The time.inc.php content is as follows:

Contact:

Mailto {mailto address= "contact@smartyllc.com" subject= "Smarty LLC Contact" encode= "javascript"}

Load test.conf:

{conf_load file= "test.conf" section= "test"} {# tt#}

The test.conf content is as follows:

[test] tt = 12122 the common simple applications of Smarty templates are shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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