Php-based Smarty basic syntax and three major variables-PHP Tutorial

Source: Internet
Author: User
Php's Smarty basic syntax and three major variables. The basic syntax and three variables of php's Smarty are mainly variables in the Smarty template, the following are the three variables related to Smarty and some common usage. First, create the basic syntax of php's Smarty and the three variables in the root directory.

The Smarty template mainly uses variables. The following are the three variables related to Smarty and some common usage

First, create the file to be used in the root directory basically the same as the previous one. configs is the configuration folder.

1. variables allocated from PHP

Use$ Smarty-> assign ()

$ Smarty-> display ()

First, write the initialization php code, which is the same as conn. inc. php in the previous article.

 Assign ("title", "11111111"); $ smarty-> assign ("content", "2222222222"); // assign an associated array, use less $ smarty-> assign (array ("author" => "", "time" => date ("Y-m-d H: I: s "); // register the function $ smarty-> registerPlugin (" function "," myfun "," test "); // register the plug-in, register the test function as myfun function test ($ args) {// args = array ("size" => 7, "color" = "yellow ", "num" => 5, "connect" => "3333333") // cyclically traverse $ str = ""; for ($ I = 0; $ I <$ args ["num"]; $ I ++) {$ str. = ''. $ args ["content"]. '';} return $ str;} // array $ smarty-> assign (" contacts ", array (" 0575-1241243 "," kefeng.hyf@gmail.com ", array (" 243214124 ", "45345"); // object class Person {public $ name = "kefeng"; function say () {return $ this-> name. "hhhhhhhh" ;}}$ smarty-> assign ("p", new Person (); $ smarty-> display ("demo.html"); // displayed Template

Demo.html:

  Content: <{$ content}> 

Content: <{$ content}>
Content: <{$ content}>
Author: <{$ author}>
Time: <{$ time}>
Time: <{myfun ()}>
Time: <{date ("y-m-d")}>

<{Myfun size = "7" color = "yellow" num = "5" content = "3333333"> <{$ contacts [0]}>
<{$ Contacts [2] [0]}>

<{$ P-> name}>
<{$ P-> say ()}>

Script

<{$ Content }><{$ author }><{$ time }><{ myfun ()}> <{date ("y-m-d")}> <{myfun size = "7" color = "yellow" num = "5" content = "3333333"> <{$ contacts [0]}> <{$ contacts [2] [0] }>< {$ p-> <{$ p->

2. read variables from the configuration file

Here we needCreate a configs folder with the configuration file test. conf.

Test. conf:

bodycolor=#3e3e3ebodybgcolor=yellowborder=10align=centerwidth=800bgcolor=gray[index]one=11111[list]two=22222[content]three=33333

Enable session in demo. php

 Display ("demo.html"); // displayed Template
 

Note: Load the configuration file: <{config_load>
Read: <{# content #}>
The second parameter is required to load the region File: <{config_load section = "index">
<{Config_load> <{# content #}> <{config_load section = "index">

Demo.html:. write some header files and load the code.

  <{Config_load "test. conf" section = "index"}> .............. <{# one #}> 

<{# Two #}>
<{# Three #}>
... <{Title = "">

III. reserved variables

Mainly include: $ _ GET
$ _ POST
$ _ SESSION
$ _ SERVER
$ _ ENV
<{$ Smarty ()}>

 

<{$ Smarty. session. username}>
<{$ Smarty. now}>
<{$ Smarty. const. ROOT}>
<{$ Smarty. const. M_PI}>
<{$ Smarty. current_dir}>



The principal variables in the Smarty template are mainly Variables. The following are the three variables related to Smarty and some common usage. First, create them in the root directory...

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.