Home Backend Development PHP Tutorial php开发之smarty1

php开发之smarty1

Jun 13, 2016 pm 12:17 PM
assign compile display path smarty

php开发之smarty一

关于smarty的配置在这里就不说了。这里就说一下其中的两个函数。
(1) assign 方法
assign 用于在模版被执行时为模版变量赋值,语法格式如下:
{assign var =”” value =”“}
参数var为被赋值的变量名,参数value是赋给变量的值。

(2)display方法
display 用来显示模版,需要制定一个合法的模版资源的类型和路径。
语法格式如下:
void display(string template[,string cache_id[,string compile_id]])
参数cache_id 为可选参数,用于指定缓存号。参数compile_id为可选参数用于指定一个编译号。编译号可以将一个模版编译成不同的版本使用。例如,可针对不同的语言编译模版。编译号的另一个作用,如果存在多个 templatedircompile_dir 编译后存档目录,这是可以为每一个 templatedirdisplay()compile_id 编译号属性来一次性设定。

示例代码如下:

<code class=" hljs xml"><span class="php"><span class="hljs-preprocessor"><?php</span> <span class="hljs-comment">//定义服务器的绝对路径</span>define(<span class="hljs-string">'BASE_PATH'</span>, <span class="hljs-variable">$_SERVER</span>[<span class="hljs-string">'DOCUMENT_ROOT'</span>]);<span class="hljs-comment">//定义smarty的绝对路径</span>define(<span class="hljs-string">'SMARTY_PATH'</span>, <span class="hljs-string">'/PHPTEST/smarty/'</span>);<span class="hljs-comment">//加载Smarty的类库文件</span><span class="hljs-keyword">require</span> BASE_PATH.SMARTY_PATH.<span class="hljs-string">'Smarty.class.php'</span>;<span class="hljs-comment">//实例化一个Smarty对象</span><span class="hljs-variable">$smarty</span> = <span class="hljs-keyword">new</span> Smarty();<span class="hljs-comment">//定义各个目录的路径</span><span class="hljs-variable">$smarty</span>->template_dir =BASE_PATH.SMARTY_PATH.<span class="hljs-string">'templates/'</span>;<span class="hljs-variable">$smarty</span>->compile_dir =BASE_PATH.SMARTY_PATH.<span class="hljs-string">'templates_c/'</span>;<span class="hljs-variable">$smarty</span>->config_dir =BASE_PATH.SMARTY_PATH.<span class="hljs-string">'configs/'</span>;<span class="hljs-variable">$smarty</span>->cache_dir =BASE_PATH.SMARTY_PATH.<span class="hljs-string">'cache/'</span>;<span class="hljs-comment">//使用Smarty 赋值方法将一对名称/方法发送到模版中</span><span class="hljs-variable">$smarty</span>->assign(<span class="hljs-string">'title'</span>,<span class="hljs-string">'Jack 的第一个Smarty程序'</span>);<span class="hljs-variable">$smarty</span>->assign(<span class="hljs-string">'content'</span>,<span class="hljs-string">'Hello welcome to study Smarty'</span>);<span class="hljs-comment">//显示模版</span><span class="hljs-variable">$smarty</span>->display(<span class="hljs-string">'index.html'</span>);<span class="hljs-preprocessor">?></span></span></code>
Copy after login

运行结果如下:

这里写图片描述

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Fujifilm X-M5 price expectations rise as leaked selfie display adds complexity to the formerly affordable camera line Fujifilm X-M5 price expectations rise as leaked selfie display adds complexity to the formerly affordable camera line Sep 07, 2024 am 09:34 AM

Fujifilm fans were recently very excited at the prospect of the X-T50, since it presented a relaunch of the budget-oriented Fujifilm X-T30 II that had become quite popular in the sub-$1,000 APS-C category. Unfortunately, as the Fujifilm X-T50's launc

Steps to set the PATH environment variable of the Linux system Steps to set the PATH environment variable of the Linux system Feb 18, 2024 pm 05:40 PM

How to set the PATH environment variable in Linux systems In Linux systems, the PATH environment variable is used to specify the path where the system searches for executable files on the command line. Correctly setting the PATH environment variable allows us to execute system commands and custom commands at any location. This article will introduce how to set the PATH environment variable in a Linux system and provide detailed code examples. View the current PATH environment variable. Execute the following command in the terminal to view the current PATH environment variable: echo$P

Samsung: New, large display based on E Ink technology displays colors and communicates wirelessly Samsung: New, large display based on E Ink technology displays colors and communicates wirelessly Jun 19, 2024 pm 03:37 PM

We frequently report on devices based on displays with electronic ink, such as e-readers. The technology offers a number of advantages: it can be read in bright environments without a backlight, and it only requires power when switching without light

Comparison: Apple Studio Display vs Samsung Smart Monitor M8 Comparison: Apple Studio Display vs Samsung Smart Monitor M8 May 11, 2023 pm 10:46 PM

Samsung Smart Display M8 vs. Apple Studio Display: Design and Size Since its launch, the Apple Studio Display has been compared to the iMac, consisting of a relatively thin panel on a relatively simple L-shaped stand. It's a well-known and well-loved aesthetic, and Samsung seems to have borrowed it for its presentation. The Samsung SmartMonitor M8 uses the same idea of ​​a thin screen on a stand that looks very similar. Some minor elements are different, such as the small section in the lower left corner that sticks out a bit and the Samsung's chin is very thin, but they seem to be close in terms of basic design. Samsung seems to have taken a lot of inspiration from the 24-inch iMac. Apple's display is smaller than Samsung's

Apple Studio Display's power cord is detachable, but requires special tools Apple Studio Display's power cord is detachable, but requires special tools May 17, 2023 pm 03:05 PM

Apple Studio Display is now officially available in stores, and many customers around the world have purchased the product. Unlike the ProDisplayXDR, the StudioDisplay has a unique power connector that appears to be non-removable. It turns out that the cable is removable, but you'll need a special tool to remove it. Apple says on its website that the Studio Display's power cord is not detachable -- and many users think so. That's because removing the cable with your hands seems impossible, but luckily the cable can be detached from the monitor. , Apple has a special tool for extracting information from its new StudioDispl

Comparison: Apple Studio Display vs LG UltraFine 5K Display: Which one is better? Comparison: Apple Studio Display vs LG UltraFine 5K Display: Which one is better? Apr 16, 2023 pm 08:25 PM

StudioDisplay and LG UltraFine5KDisplay occupy similar positions in the market, but Apple's monitor is $300 more expensive. Here's everything you need to know about how these monitors compare. Six years is a long time in the tech world, and it's also the time since Apple has sold a branded monitor that costs less than $5,000. During this time, Apple partnered with LG to sell the LG UltraFine series, which catered specifically to Mac users. In 2019, Apple stopped selling these LG monitors in favor of ProDisplayXDR, an affordable Mac-friendly display

display what does it mean display what does it mean Oct 26, 2023 am 11:50 AM

Display usually refers to the operation or function of displaying data, information or results to the user in some way or outputting it to a screen or other device. Specific meaning: 1. In the command line interface (CLI), display may refer to outputting data in text, tables or other formats to the terminal window for users to view or analyze; 2. In the graphical user interface (GUI), Display may refer to displaying images, text, charts and other content on the application window or interface for user interaction or browsing, etc.

Boot Camp updated to support Apple Studio Display Boot Camp updated to support Apple Studio Display May 20, 2023 pm 11:34 PM

IntelMac users running Windows on a Mac can now update their drivers in BootCamp to support Apple's StudioDisplay. Apple regularly updates BootCamp to introduce support for new hardware, as well as typical compatibility and performance improvements. In the March software update, Apple has enabled BootCamp to work with the new StudioDisplay. The update that brings BootCamp to version 6.1.17 introduces two key support elements. First, it adds compatibility with StudioDisplay, ensuring

See all articles