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