这篇文章主要介绍了smarty模板引擎中变量及变量修饰器用法,实例分析了smarty模板引擎中常见变量、变量修饰器及常量的具体用法,需要的朋友可以参考下
本文实例讲述了smarty变量及变量修饰器的应用。分享给大家供大家参考。具体如下:
模板文件:temp.htm:
复制代码 代码如下:
{config_load file="foo.conf"}
{$name.na1|cat:$name['na2']}
{$name['na1']|cat:'与'|cat:$name.na2}
{foreach from=$name item=na}
{$na}
{/foreach}
{$dog->leee()}{$dog->name}
<script><br />
{literal}<br />
function foobar{<br />
alert('foobar!');<br />
}<br />
{/literal}<br />
</script>
dosomething | 帅锅 |
{$smarty.server.SERVER_NAME}
{$str|count_words}
-------常量--------
{$smarty.now}
{$smarty.const.MY_CONST}
{$smarty.template}
{$smarty.current_dir}
{$smarty.version}
{$smarty.ldelim|cat:$smarty.rdelim}
{$smarty.now|date_format:$config}
{$yesterday|date_format:'Y-m-d'}
{$string|default:'default变量修饰:smarty学习'}
{$str1|escape:'html'}
{$str2|escape:'mail'}
{$str1|indent|upper}
{$str1|nl2br}
{$str1|regex_replace:"/@\d{3}/":"ABC"}
{$str1|replace:"163":"sina"}
{$str1|spacify}
{$number|string_format:"%.2f"}
{$number|string_format:"%d"}
{$str3|strip:"|"}
去除包含在之间的字符:{$str3|strip_tags}
截取长度:{$str3|truncate:10:"...":true}
按长度换行:{$str3|wordwrap:30:"
"}
{append var='name' value="Bob" index="first"}
{append var='name' value="John" index="last"}
{$name.last}
{foreach from=$family item=home}
{foreach from=$home item=person}
{$person}
{/foreach}
{/foreach}
{$family[1].girl}
{assign var="name" value="张三丰"}
{$name}
php文件:index.php
复制代码 代码如下:
require_once('libs/Smarty.class.php');
$smarty = new Smarty();
$smarty->setTemplateDir($_SERVER['DOCUMENT_ROOT']."/php/templates/");
$smarty->setCompileDir($_SERVER['DOCUMENT_ROOT']."/php/templates_c/");
$smarty->setCacheDir($_SERVER['DOCUMENT_ROOT']."/php/cache/");
$smarty->caching = false;
$arr = array("na1"=>"帅锅","na2"=>"美女");
$smarty->assign("name",$arr);
class Dog{
public $name;
public $age;
function leee(){
return $this->name."在干吗";
}
}
$dog = new Dog();
$dog->name="小狗";
$smarty->assign("dog",$dog);
//$str = "hello world,i am here. i love smarty!";
$str = "帅锅";
$str1 = "新浪 And\n aassu@163.com";
$str2 = "aassu@163.com";
$smarty->assign("str",$str);
$smarty->assign("str1",$str1);
$smarty->assign("str2",$str2);
$smarty->assign("number",30.293934);
$smarty->assign("str3","akie abfal ,dooerw,,show databases,desc table");
$config = "Y-m-d H:i:s";
$smarty->assign("config",$config);
$smarty->assign("yesterday",strtotime('-1 day'));
//常量
define("MY_CONST","百度");
//append成员方法的使用
$family = array("husband"=>"帅锅","wife"=>"美女");
$famiadd = array("boy"=>"张三丰","girl"=>"王昭君");
$smarty->append("family",$family);
$smarty->append("family",$famiadd);
echo "
";<br> print_r($family);<br> $smarty->display("temp.htm");<br> ?> <p>希望本文所述对大家的php程序设计有所帮助。</p>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software