Detailed explanation of the usage and examples of variables in PHP template engine Smarty

墨辰丷
Release: 2023-03-29 16:02:01
Original
1338 people have browsed it

This article mainly introduces the use of variables in the PHP template engine Smarty. It details the principles of Smarty templates, downloading, configuration methods and variable usage skills. Friends in need can refer to it

1. Overview:

Smarty is one of the many template engines in PHP. It is a class library written based on PHP.
Advantages of Smarty:
1. Optimize website access speed;
2. Separate web front-end design and program;

2. Installation of Smarty

1. You need to download the latest Smarty version from Smarty’s official website http://www.smarty.net/download.php. For example, the downloaded version is: Smarty-2.6.18.tar.tar;

2. Unzip the Smarty-2.6.18.tar.tar compressed package and you will find many files and folders. Except for the libs folder, deleting all others will be useless;

3. When When calling the Smarty template engine, you should first use PHP's require statement to load the file libs/Smarty.class.php.

3. Default settings of Smarty class library

require After entering the Smarty.class.php file, if you need to set the members in the Smarty class library, there are two options: There are two methods: one is to modify it directly in the Smarty.class.php file; the other is to re-specify it after initializing the class library, and the latter is generally used. The following is an explanation of the member attributes in the Smarty class library:

1, $template_dir: Set the directory where the template files in the website are stored, the default directory is templates
2, $compile_dir: Set the compilation in the website The directory where the file is stored, the default directory is templates_c
3, $config_dir: defines the directory used to store special configuration files of templates, the default is configs
4, $left_delimiter: used for the left terminator variable in the template, default It is '{'
5, $right_delimiter: used for the right terminator variable in the template, the default is '}'

4. Use of variables:

## All access in #Smarty is based on variables. The following is an example to illustrate.

Example idea: The main file introduces the template initialization configuration file (init.inc.php) and a class, and assigns values to the variables in the template to display.

First, set the init.inc.php file as the initialization configuration file of the Smarty template

init.inc.php

template_dir = ROOT_PATH.'/tpl/'; //重新设置模板目录为根目录下的 tpl 目录 $_tpl->compile_dir = ROOT_PATH.'./com/'; //重新设置编译目录为根目录下的 com 目录 $_tpl->left_delimiter = '<{'; //重新设置左定界符为 '<{' $_tpl->right_delimiter = '}>'; //重新设置左定界符为 '}>' ?>
Copy after login

Main file index.php

来访问的 * */ $_tpl->assign('title',$title); $_tpl->assign('content',$content); //变量的赋值 $_tpl->assign('arr1',array('abc','def','ghi')); //索引数组的赋值 $_tpl->assign('arr2',array(array('abc','def','ghi'),array('jkl','mno','pqr'))); //索引二维数组的赋值 $_tpl->assign('arr3',array('one'=>'111','two'=>'222','three'=>'333')); //关联数组的赋值 $_tpl->assign('arr4',array('one'=>array('one'=>'111','two'=>'222'),'two'=>array('three'=>'333','four'=>'444'))); //关联二维数组的赋值 $_tpl->assign('arr5',array('one'=>array('111','222'),array('three'=>'333','444'))); //关联和索引混合数组的赋值 $_tpl->assign('object',new Persion('小易', 10)); //对象赋值 //Smarty 中数值也可以进行运算(+-*/^……) $_tpl->assign('num1',10); $_tpl->assign('num2',20); $_tpl->display('index.tpl'); ?>
Copy after login

Template file index.tpl of the main file index.php (stored in the /tpl/ directory)

    <{$title}> 
变量的访问:<{$content}>
索引数组的访问:<{$arr1[0]}> <{$arr1[1]}> <{$arr1[2]}>
索引二维数组的访问: <{$arr2[0][0]}> <{$arr2[0][1]}> <{$arr2[0][2]}> <{$arr2[1][0]}> <{$arr2[1][1]}> <{$arr2[1][2]}>
关联数组的访问:<{$arr3.one}> <{$arr3.two}> <{$arr3.three}>
关联二维数组的访问:<{$arr4.one.one}> <{$arr4.one.two}> <{$arr4.two.three}> <{$arr4.two.four}>
关联和索引混合数组的访问:<{$arr5.one[0]}> <{$arr5.one[1]}> <{$arr5[0].three}> <{$arr5[0][0]}>
对象中成员变量的访问:<{$object->name}> <{$object->age}>
对象中方法的访问:<{$object->hello()}>
变量的运算:<{$num1+$num2}>
变量的混合运算:<{$num1+$num2*$num2/$num1+44}>
Copy after login

Persion.class.php

name = $name; $this->age = $age; } //定义一个 hello() 方法,输出名字和年龄 public function hello() { return '您好!我叫'.$this->name.',今年'.$this->age.'岁了。'; } } ?>
Copy after login

Execution result:

变量的访问:This is body content! 索引数组的访问:abc def ghi 索引二维数组的访问: abc def ghi jkl mno pqr 关联数组的访问:111 222 333 关联二维数组的访问:111 222 333 444 关联和索引混合数组的访问:111 222 333 444 对象中成员变量的访问:小易 10 对象中方法的访问:您好!我叫小易,今年10岁了。 变量的运算:30 变量的混合运算:94
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Detailed explanation of several methods for PHP to obtain user access IP addresses

Commonly used array operations in PHP Detailed explanation of the method

phpFunction uses a variable number of parameter methods

The above is the detailed content of Detailed explanation of the usage and examples of variables in PHP template engine Smarty. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!