smarty模板引擎从php中获取数据的方法,smarty模板
smarty模板引擎从php中获取数据的方法,smarty模板
本文实例讲述了smarty模板引擎从php中获取数据的方法。分享给大家供大家参考。具体如下:
smarty可以分配($smarty->assign)的变量类型:所有php支持的数据类型——基本数据类型、复合数据类型、特殊数据类型(具体见smarty相关手册)。
操作/显示文件:index.php
复制代码 代码如下:
//创建smarty对象
require_once("./libs/Smarty.class.php");
$smarty = new Smarty();
$smarty->assign("aa","hello word");//分配字符串
$smarty->assign("bb",123);//分配整型
$smarty->assign("cc",90.8);//分配float型,浮点型
$smarty->assign("dd",true);//分配字符串
//分配数组,数组一般从数据库取出,这里直接给数组
$arr1 = array("北京","上海","广州");//索引数组
$smarty->assign("arr1",$arr1);//分配索引数组
$arr2 = array("city1"=>"北京","city2"=>"上海","city3"=>"广州");//关联数组
$smarty->assign("arr2",$arr2);//分配关联数组
$arr3 = array(array("北京","上海","广州"),array("关羽","张飞","美女"));
$smarty->assign("arr3",$arr3);
$arr4 = array("aa"=>array("北京","上海","广州"),"bb"=>array("关羽","张飞","美女"));
$smarty->assign("arr4",$arr4);
//对象类型
class Master{
public $name;
public $address;
}
$master = new Master();
$master->name="百度";
$master->address = "中关村";
class Dog{
public $name;
public $age;
public $color;
public $arr;
public $master;
function __construct($name,$age,$color,$arr){
$this->name = $name;
$this->age = $age;
$this->color = $color;
$this->arr = $arr;
}
}
$dog = new Dog("小狗",4,"金黄色",$arr2);
$dog->master = $master;
$smarty->assign("dog",$dog);
$smarty->display("index.tpl");
?>
模板文件:index.tpl
复制代码 代码如下:
smarty变量操作
取字符串:{$aa}
取整数:{$bb}
取浮点型:{$cc}
取布尔值:{$dd}
取数组(索引数组):{$arr1[0]}--{$arr1[1]}--{$arr1[2]}
取数组(关联数组):{$arr2.city1}--{$arr2.city2}--{$arr2.city3}
取二组数组(索引,取单个):{$arr3[0][0]}
取二组数组(索引,遍历全部):
取二维数组(关联):{$arr4.aa[2]}
取二维数组(关联、遍历):
取对象(普通属性):{$dog->name}
取对象(数组属性):{$dog->arr.city1}
取对象(对象属性):{$dog->master->name}
希望本文所述对大家的php程序设计有所帮助。

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP email templates: Customize and personalize your email content With the popularity and widespread use of email, traditional email templates can no longer meet people's needs for personalized and customized email content. Now we can create customized and personalized email templates by using PHP programming language. This article will show you how to use PHP to achieve this goal, and provide some specific code examples. 1. Create an email template First, we need to create a basic email template. This template can be an HTM

Regarding PPT masking, many people must be unfamiliar with it. Most people do not understand it thoroughly when making PPT, but just make it up to make what they like. Therefore, many people do not know what PPT masking means, nor do they understand it. I know what this mask does, and I don’t even know that it can make the picture less monotonous. Friends who want to learn, come and learn, and add some PPT masks to your PPT pictures. Make it less monotonous. So, how to add a PPT mask? Please read below. 1. First we open PPT, select a blank picture, then right-click [Set Background Format] and select a solid color. 2. Click [Insert], word art, enter the word 3. Click [Insert], click [Shape]

C++ template specializations affect function overloading and rewriting: Function overloading: Specialized versions can provide different implementations of a specific type, thus affecting the functions the compiler chooses to call. Function overriding: The specialized version in the derived class will override the template function in the base class, affecting the behavior of the derived class object when calling the function.

According to news on October 8, the U.S. auto market is undergoing a change under the hood. The previously beloved six-cylinder and eight-cylinder power engines are gradually losing their dominance, while three-cylinder engines are emerging. News on October 8 showed that the U.S. auto market is undergoing a change under the hood. The beloved six-cylinder and eight-cylinder power engines in the past are gradually losing their dominance, and the three-cylinder engine is beginning to emerge. In most people's minds, Americans love large-displacement models, and the "American big V8" has always been the Synonymous with American cars. However, according to data recently released by foreign media, the landscape of the U.S. auto market is undergoing tremendous changes, and the battle under the hood is intensifying. It is understood that before 2019, the United States

C++ is a programming language widely used in various fields. Its template metaprogramming is an advanced programming technique that allows programmers to transform types and values at compile time. Template metaprogramming is a widely discussed topic in C++, so questions related to it are quite common in interviews. Here are some common template metaprogramming interview questions in C++ that you may be asked. What is template metaprogramming? Template metaprogramming is a technique for manipulating types and values at compile time. It uses templates and metafunctions to generate based on types and values

The intelligent NPC created by Academician Huang in "Cyberpunk 2077" can already speak Chinese? Qubit's first-hand experience, witnessing NPCs conversing fluently in both Chinese and English, with natural expressions and movements, and matching mouth shapes... If there wasn't a screen in front of me, it would really feel like being there. . At this year's CES exhibition, Nvidia used its intelligent engine Avatar Cloud Engine (ACE) to make game NPCs "alive", which caused quite a shock. △The intelligent NPC displayed at CES uses ACE. The characters in the game can have realistic voice conversations with players, while showing vivid expressions and body movements without having to prepare a script in advance. At the time of its debut, there were Ubisoft, Tencent, NetEase, MiHoYo and other countries.

How to implement image template and mask processing in Vue? In Vue, we often need to perform some special processing on images, such as adding template effects or masks. This article will introduce how to use Vue to achieve these two image processing effects. 1. Image template processing When using Vue to process images, we can use the filter attribute of CSS to achieve template effects. The filter attribute adds graphic effects to the element, and the brightness filter can change the brightness of the picture. we can change

Real-time global illumination (Real-time GI) has always been the holy grail of computer graphics. Over the years, the industry has proposed various methods to solve this problem. Common methods include constraining the problem domain by utilizing certain assumptions, such as static geometry, a rough scene representation, or tracking rough probes, and interpolating lighting between the two. In Unreal Engine, the global illumination and reflection system Lumen technology was co-founded by Krzysztof Narkowicz and Daniel Wright. The goal was to build a solution that was different from its predecessors, capable of achieving uniform lighting and a baked-like lighting quality. Recently, at SIGGRAPH 2022, Krzysztof Narko
