Common Simple Application Analysis of Smarty templates and smarty Application Analysis

Source: Internet
Author: User

Common Simple Application Analysis of Smarty templates and smarty Application Analysis

This document analyzes common simple applications of Smarty templates. We will share this with you for your reference. The details are as follows:

First, you must include the smarty class:

include_once '../libs/Smarty.class.php';

Then create a Smarty object:

$smarty = new Smarty;

You can customize the start and end characters of Smarty. The default value is {}

$ Smarty-> left_delimiter = '<'; // The left symbol is <$ smarty-> right_delimiter = '>'; // The right symbol is>

The most important method is assign, for example:

$ Smarty-> assign ('test', $ te); // pay the value of $ test to test. In the template page, tpl uses {$ test}

Another example is $ arr = array (1, 2, 3). The assignment is still like this:

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

However, you must use foreach or section when displaying the template page. The usage of foreach 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 section usage is as follows:

{Section name = 'test' loop = $ arr} {$ smarty. section. name. iteration} // serialize the output. The sequence number starts from 1 and the index starts from 0. {$ arr [test]} {/section}

Do not forget the most important step:

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

The following describes how to display some common things in a template.

1. Connection operation:

My name is {$ str1 | cat: "Li Bai"}; // The output result is: My name is $ str1 Li Bai.

2. Current date:

{$ Str2 | rdate_format: "Y %-m %-d %"} // format the output result $ str2 date, as shown in 0000-00-00

3. indent:

{$ Str3 | indent: 8: "*"} // before $ str3, the first indentation is 8. * spaces are indented by default.

4. Case sensitivity:

{$ Str4 | lower} // $ str4 in lower case {$ str4 | upper} // $ str4 in upper case

Filter:

{$ Url | escape: "url"} // replace <tr bgcolor = '{cycle values = "# EBEBEB, # ACABAB "} '> // The tr background alternate colors are # ebeb, # ACABAB

Matching and 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.

Contains the header template file:

{include file="top.tpl"}

Call the functions in time. inc. php:

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

The content of time. inc. php is as follows:

<? Php function smarty_insert_getCurrentTime {return gmdate ('l, j f y g: I a t'); // obtain the current date and time}?>

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 content of test. conf is as follows:

[test]tt = 12122

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.