Home php教程 php手册 smarty模板引擎基础知识入门

smarty模板引擎基础知识入门

Jun 06, 2016 pm 08:05 PM
smarty basic knowledge template engine

这篇文章主要介绍了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文件

{$title} {$content}

这里面的内容是用占位符的形式,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.运行结果

标题和内容都显示出来了:

smarty模板引擎基础知识入门

结论:

真正运行的文件,既不是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程序设计有所帮助。

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
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)

What are the common template engines in PHP programming? What are the common template engines in PHP programming? Jun 12, 2023 am 09:50 AM

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

Learn to use the Golang template engine: A basic guide to using templates in Golang Learn to use the Golang template engine: A basic guide to using templates in Golang Jan 20, 2024 am 10:13 AM

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.

ThinkPHP6 template engine usage guide: Create an exquisite front-end interface ThinkPHP6 template engine usage guide: Create an exquisite front-end interface Aug 26, 2023 pm 11:09 PM

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

How to use template engine Blade in Fat-Free framework? How to use template engine Blade in Fat-Free framework? Jun 03, 2023 pm 08:40 PM

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

Template Engine in Go: A Complete Guide Template Engine in Go: A Complete Guide Jun 17, 2023 pm 12:55 PM

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".

A must-read for learning MySQL! Detailed explanation of the basic knowledge of SQL statements A must-read for learning MySQL! Detailed explanation of the basic knowledge of SQL statements Jun 15, 2023 pm 09:00 PM

MySQL is an open source relational database management system that is widely used in web application development and data storage. Learning MySQL's SQL language is very necessary for data managers and developers. SQL language is the core part of MySQL, so before learning MySQL, you need to have a full understanding of SQL language. This article aims to explain the basic knowledge of SQL statements to you in detail, so that you can understand SQL statements step by step. SQL is the abbreviation of Structured Query Language, which is used in relational data

Best Template Engine in PHP Best Template Engine in PHP May 24, 2023 pm 04:01 PM

PHP is a language widely used in web development. Whether you are developing a small website or a large system, PHP is very popular and convenient. In the PHP development process, we need to separate the logic and data layers, which requires the use of a template engine. The template engine can be simply understood as merging data and template files to generate the final HTML file. In this article, we will introduce some of the best template engines available in PHP. SmartySmarty is one of the most popular template engines in PHP,

How to use PHP and Smarty to achieve front-end and back-end separation development How to use PHP and Smarty to achieve front-end and back-end separation development Jun 25, 2023 pm 01:46 PM

In modern web development, the separation of front-end and back-end has become a very popular trend, which allows developers to better organize projects and improve the efficiency of project development. PHP and Smarty are two very commonly used technologies, which can be used to achieve front-end and back-end separation development. This article will introduce how to use PHP and Smarty to achieve front-end and back-end separation development. What is front-end and back-end separation development? In traditional web development, the front-end is mainly responsible for the presentation of the page and the logic of interaction with the back-end. The backend is mainly responsible for the business

See all articles