smarty模板引擎基础知识入门
这篇文章主要介绍了smarty模板引擎基础知识入门,较为详细的分析了smarty的基本概念并实例分析了相关的基本用法,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了smarty模板引擎基础知识。分享给大家供大家参考。具体如下:
一、基本概念
1.什么是mvc?
mvc是一种开发模式,核心思想是:数据的输入、数据的处理、数据显示的强制分离。
2.什么是smarty?
smarty是一个php的模板引擎。更明确的来说,它可以帮助开发者更好的分离程序逻辑和页面显示。
3.smarty运行原理
模板文件,就是一个显示数据的模板,其中需要显示的数据用占位符代替。
smarty运行时,会读取模板文件,将模板文件中的占位符替换成真正的数据,并输出一个处理后的php文件,,交由服务器运行。
二、自己写一个smarty模板
为了更好的理解smarty模板,现在自己先写一个自己的smarty模板-minismarty,让自己更加深入的了解smarty运行原理。
1.新建项目minismarty
新建模板文件路径:templates
新建模板文件被编译后的文件路径:templates_c
新建模板文件:intro.tpl
新建运行的文件:index.php
新建自己的smarty,即处理模板的文件:cls_MiniSmarty.php
2.编写index.php文件
assign("title","hello minismarty!"); $miniSmarty->assign("content","this is content!"); //传递数据到哪个页面显示 $miniSmarty->display("intro.tpl"); ?>
3.编写intro.tpl文件
è¿éé¢çå
容æ¯ç¨å ä½ç¬¦çå½¢å¼ï¼smartyçä½ç¨å°±æ¯å°å ä½ç¬¦çå
å®¹æ¿æ¢æçæ£çæ°æ®ã
è¿æ ·å°±å¯ä»¥å®ç°æ¨¡æ¿æä»¶åæ°æ®æä»¶å¼ºå¶å离ï¼éè¿smartyè¿è¡æ°æ®çä¼ éã
4.ç¼åcls_MiniSmarty.phpæä»¶
tpl_vars[$tpl_var] = $var; //å°æ°æ®æ·»å å°æ°ç»ä¸ } } /** * æ¾ç¤ºæ°æ® * åæ°1ï¼æ¾ç¤ºå°åªä¸ªæ¨¡æ¿æä»¶ä¸ */ function display($tpl_file) { //è·å¾æ¨¡æ¿æä»¶çè·¯å¾ $tpl_file_path = $this->template_dir . $tpl_file; //è·å¾æ¨¡æ¿æä»¶è¢«ç¼è¯åçæä»¶è·¯å¾ $compile_file_path = $this->templates_c_dir . "com_" . $tpl_file . ".php"; //夿æä»¶æ¯å¦åå¨ if (!file_exists($tpl_file_path)) { return false; } //ä¸ç¨æ¯æ¬¡é½çæç¼è¯æä»¶,åªæç¼è¯æä»¶ä¸åå¨æè æ¨¡æ¿æä»¶è¢«ä¿®æ¹äºæçææ°çç¼è¯æä»¶ //ç¸å½äºç¼åäºç¼è¯æä»¶ //filemtime彿°ï¼è·å¾æä»¶ççææ¶é´ if (!file_exists($compile_file_path) || filemtime($tpl_file_path) > filemtime($compile_file_path)) { //è¯»åæ¨¡æ¿æä»¶çå 容 $fpl_file_content = file_get_contents($tpl_file_path); $newStr = myReplace($fpl_file_content); //å°æ¿æ¢åçåç¬¦ä¸²çææ°çæä»¶ï¼ä¹å°±æ¯ç¼è¯åçæä»¶ file_put_contents($compile_file_path, $newStr); } //å¼å ¥ç¼è¯åçæä»¶ include $compile_file_path; } /** * å¯¹æ¨¡æ¿æä»¶ä¸çå 容è¿è¡æ¿æ¢ï¼è·å¾æ°çå符串 */ function myReplace($fpl_file_content) { $pattern = array ( '/\{\s*\$([a-zA-Z_][a-zA-Z0-9_]*)\s*\}/i' ); $replace = array ( 'tpl_vars["${1}"] ?>' ); $newStr = preg_replace($pattern, $replace, $fpl_file_content); return $newStr; } } ?>
preg_replaceæ¹æ³ä»ç»ï¼
åæ°1ï¼æ¿æ¢çè§å
åæ°2ï¼æ¿æ¢æçå
容
åæ°3ï¼æ¿æ¢æä½çå
容
5.è¿è¡ç»æ
æ é¢åå 容齿¾ç¤ºåºæ¥äº:
ç»è®ºï¼
çæ£è¿è¡çæä»¶ï¼æ¢ä¸æ¯index.phpï¼ä¹ä¸æ¯intro.phpï¼èæ¯äºè
éè¿smartyä½ç¨åçæä»¶:
com_intro.tpl.phpãè¿ä¸ªæä»¶ä¸æ°æ®æ¥æºäºindex.phpï¼æ¾ç¤ºçå¸å±æ¥èªintro.tplï¼ä¸é´çæ¡¥æ¢æ¯smartyã
smartyçä½ç¨æ¯æ¥åæ°æ®ãå¡«å
æ°æ®ï¼æ¿æ¢æ¨¡æ¿ä¸çå ä½ç¬¦ï¼ãå¹¶å è½½æ¿æ¢åçæä»¶ã
ä¸ã讲解smarty使ç¨ç»è
1.å¦ä½é ç½®smarty?
è§£ååï¼å°libsæä»¶å¤¹æ·è´å°é¡¹ç®ç®å½ä¸å³å¯ï¼ç¶ååå建2个æä»¶å¤¹templatesåtemplates_cï¼å嫿¾æ¨¡æ¿æä»¶å模æ¿ç¼è¯åæä»¶ã
2.使ç¨smarty注æäºé¡¹
â æ¿æ¢åéçæ ç¤ºç¬¦ã
å 为é»è®¤ç表示符æ¯{}è¿ä¸ªåstyleæ ·å¼ä¸ç{}åçå²çªï¼æä»¥éè¦ä¿®æ¹ä¸ä¸é»è®¤çæ è¯ç¬¦ï¼ä¸è¬ä¿®æ¹ä¸ºï¼{}
â¡ä¿®æ¹æ è¯ç¬¦çæ¹æ³ã
æ¹æ³ä¸ï¼ç´æ¥ä¿®æ¹smartyç±»æºç ï¼ä¸æ¨èã
æ¹æ³äºï¼ä½¿ç¨smartyæä¾çæ¹æ³è¿è¡ä¿®æ¹ã
$smarty->left_delimiter="{right_delimiter=">}";
â¢smartyçä¸äºåºæ¬é
ç½®
$smarty->template_dir="./templates";//模æ¿è·¯å¾ $smarty->compile_dir="./templates_c";//ç¼è¯è·¯å¾ $smarty->caching=false; //æ¯å¦ä½¿ç¨ç¼å $smarty->cache_dir="./smarty_cache";//å¦æä½¿ç¨ç¼åçè¯ï¼ç¼åçè·¯å¾
3.smartyæ¨¡æ¿ææ¯åé åéçç»èé®é¢
ä¸å¥è¯ï¼å¯ä»¥åé
phpæ¯æçåç§æ°æ®ã
phpåºæ¬æ°æ®ï¼int double string bool
å¤åæ°æ®ç±»åï¼array object
ç¹æ®æ°æ®ç±»åï¼resource null
å¸ææ¬ææè¿°å¯¹å¤§å®¶ç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

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

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











In recent years, the template engine in PHP programming has become an important part of PHP development, making it easier for programmers to develop and manage pages. This article will introduce common template engines in PHP programming. SmartySmarty is a commonly used PHP template engine. It supports a series of functions such as cached templates, plug-in modules and custom functions. Smarty's syntax is very flexible and can solve the problem of combining PHP variables with HTML tags, making the PHP language more suitable for templated design. Moreover, S

ThinkPHP6 template engine usage guide: Create an exquisite front-end interface Introduction: With the development of web applications, the design and development of front-end interfaces have become increasingly important. As a developer, we need to use a powerful template engine to help us create and manage front-end interfaces. ThinkPHP6's template engine is a powerful tool to meet this need. This article will introduce how to use the ThinkPHP6 template engine to create a beautiful front-end interface. Part 1: Install ThinkPHP6 template engine

Fat-Free Framework is a lightweight PHP framework designed to provide simple and flexible tools for building web applications. It contains many useful features such as routing, database access, caching, etc. In the Fat-Free framework, using the Blade template engine can help us manage and render templates more conveniently. Blade is the template engine in the Laravel framework, which provides powerful syntax and template inheritance capabilities. In this article I will demonstrate how to use Bl in Fat-Free framework

Sharing experience on template engine selection and use in JavaScript development Introduction: In modern front-end development, the template engine (TemplateEngine) plays a crucial role. They enable developers to organize and manage large amounts of dynamic data more efficiently and effectively separate data from interface presentation. At the same time, choosing an appropriate template engine can also bring developers a better development experience and performance optimization. However, among the many JavaScript template engines, which one should you choose? catch

With the development of Internet technology, the demand for web applications is also increasing. Web developers often use template engines to generate dynamic web pages. This article will explore a new template engine: the Go language template engine. What is the Go language template engine? Go language is an advanced programming language developed by Google. Its syntax is concise and clear, making it easy to learn and use. The Go language template engine is a template system used to generate HTML templates in the Go language. The Go language template engine is called the "standard library".

Golang Template Engine Getting Started Guide: How to use templates in Golang, specific code examples are required Introduction: A template engine is a tool that can combine data and templates and generate documents in HTML, text or other formats. In Golang, we can use the built-in template package (html/template) to implement the functions of the template engine. This article will introduce in detail how to use the template engine in Golang and provide specific code examples. 1. The basic concept of template engine is to understand how to use it.

PHP and CGI template engines: How to achieve website reusability Introduction: When developing websites, we often need to deal with the display of dynamic content. In order to achieve code maintainability and reusability, using a template engine is a wise choice. This article will introduce PHP and CGI, two commonly used template engines, and show how to use them to achieve website reusability through code examples. 1. PHP template engine PHP is a widely used server scripting language with flexibility and powerful functions. PHP template engine is a

Nowadays, website development is inseparable from an important component-template engine. A template engine refers to a tool that combines page templates and data to generate HTML code with a specific format. In various website development frameworks, the template engine is an essential component, because the template engine can greatly reduce the duplication of code and improve the dynamics of the page. One of the most common and popular template engines is Smarty. Smarty is a DSL (DomainSpecif
