Home >Backend Development >PHP Tutorial >Smarty basic syntax and three major variables in php_PHP tutorial

Smarty basic syntax and three major variables in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:01:01813browse

Smarty basic syntax and three major variables of php

Smarty templates are mainly based on variables. Here are the three major variables of Smarty and some common usages

First, create the files to be used in the root directory, which are basically the same as last time. 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

<!--?php
    include "init.inc.php";
    //assign方法
    $smarty--->assign("title","11111111");
    $smarty->assign("content","2222222222");
    //分配一个关联数组,用的较少
    $smarty->assign(array("author"=>"何栘烽","time"=>date("Y-m-d H:i:s")));

    //注册函数
    $smarty->registerPlugin("function","myfun","test");  //注册插件,将test函数注册成myfun
    function test($args){
    	//args=array("size"=>7, "color"="yellow", "num"=>5, "connect"=>"3333333")
    	//循环遍历出
    	$str="";
    	for($i=0; $i<$args["num"]; $i++){
    		$str.=&#39;&#39;.$args["content"].&#39;
&#39;;
    	}
    	return $str;
    }
    //数组
    $smarty->assign("contacts",array("0575-1241243","kefeng.hyf@gmail.com", array("243214124", "45345")));
    //对象
    class Person{
    	public $name="kefeng";
    	function say(){
    		return $this->name."hhhhhhhh";
    	}
    }
    $smarty->assign("p",new Person());
    $smarty->display("demo.html");  //显示的模板

demo.html:




content:ed534ca51c0bc14419da32c9e2235506
content:ed534ca51c0bc14419da32c9e2235506
content:ed534ca51c0bc14419da32c9e2235506
author:6dbd38817b457f2a3ceaaf1e07df3f75
time:fdd557d99d69488841f963a7acaeb8f4
time:f0bef7d79e6fc4e0a1dbf6e7f85c866f
time:011f78db2dcee9d89089b11ce9f302de
Smarty basic syntax and three major variables in php_PHP tutorial
dd89b2b6d41f103433cbae2cdd49a3ac 1262a5626cdb57f8bfdb8a71f5777c60
d2a819925244fe956271df17ea74b3eb

68cbac42eae5120706a18a6374baa66cname}>
68cbac42eae5120706a18a6374baa66csay()}>

2cacc6d41bbb37262a98f745aa00fbf0

ed534ca51c0bc14419da32c9e2235506ed534ca51c0bc14419da32c9e2235506ed534ca51c0bc14419da32c9e22355066dbd38817b457f2a3ceaaf1e07df3f75fdd557d99d69488841f963a7acaeb8f4f0bef7d79e6fc4e0a1dbf6e7f85c866f< ;{date("y-m-d")}>dd89b2b6d41f103433cbae2cdd49a3ac1262a5626cdb57f8bfdb8a71f5777c60c84b3559b1e7eba09d719240bd542f4468cbac42eae5120706a18a6374baa66c68cbac42eae5120706a18a6374baa66c

2. Read variables from the configuration file

Here you need to create a configs folder with the configuration file test.conf

test.conf:

bodycolor=#3e3e3e
bodybgcolor=yellow
border=10
align=center
width=800
bgcolor=gray

[index]
one=11111

[list]
two=22222

[content]
three=33333

Open session in demo.php

<!--?php include="" smarty-=""-->display("demo.html");  //显示的模板
<!--?php-->

Note: Load the configuration file: fa7a8c7840fbe9fb22722ebfeb11f235
Read:d29b9b54978ae4fe8cba6f3ef3d715e3
The second parameter is used to load the zone file: 4a6d016a925740c885c519d22d3901b5
fa7a8c7840fbe9fb22722ebfeb11f235d29b9b54978ae4fe8cba6f3ef3d715e3d48d77c1a98479a300a419ed66d1dc18

demo.html: Here write some header files and loading code




41cdc44402e1bf88b4241fe91b363953....... ....... 9b0fdaaed84685845d4cde100b4404ec
76e2455f374f2bfa99aaecd62c184ec3
196263a863d3bad3b394d58308c21b06
......ddc5273f3fc105de06319c035f431b70

3. Reserved variables

Mainly include: $_GET
$_POST
$_SESSION
$_SERVER
$_ENV
1256b4c8edd9581360f57ad988f16334

 

a32dcd3f718e8dfc961d36181ffb6cb2
504e846c81a555eed2b9ff0768d0dcd6
bfa9c68c993788247c3bf060e36b4bf8
3cd23405a4ab0264b4dbddeeb0678e98
4a6ec2bdbba03bf520669b9b0f25db5d



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/972680.htmlTechArticlePHP’s Smarty basic syntax and three major variables are mainly variables in Smarty templates. Here are the relevant Smarty's three major variables and some common usages are first established in the root directory...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn