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
Release: 2016-07-13 10:01:01
Original
812 people have browsed it

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");  //显示的模板
Copy after login

demo.html:

Copy after login
Copy after login
content:<{$content}>
content:<{$content}>
content:<{$content}>
author:<{$author}>
time:<{$time}>
time:<{myfun()}>
time:<{date("y-m-d")}>
Smarty basic syntax and three major variables in php_PHP tutorial
<{myfun size="7" color="yellow" num="5" content="3333333"> <{$contacts[0]}>
<{$contacts[2][0]}>

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



<{$content}><{$content}><{$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 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
Copy after login

Open session in demo.php

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

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

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

Copy after login
Copy after login
<{config_load "test.conf" section="index"}>....... ....... <{#one#}>
<{#two#}>
<{#three#}>
......<{ title="">

3. Reserved variables

Mainly include: $_GET
$_POST
$_SESSION
$_SERVER
$_ENV
<{$smarty()}>

 
Copy after login

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



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...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template