


Caterpillar teaches you to write your own template engine_PHP tutorial
#phpchina首发# Smarty一直被人视为是多余的东西,我觉得认为Smarty多余的人才是多余的....不说这些了。今天我就教大家写个模板引擎,让大家都可以写一个属于自己的模板引擎,而且看完这篇文章之后,你对Smarty的认识会更进一步的。我的模板引擎名叫Stupid("傻瓜"的意思),我不喜欢太聪明的东西! 好了,我们现在就先编写stupid.class.php吧。 下面定义了两个常量,用来存放模板文件夹和编译文件夹的: 开始编码了>>> define(TPL_DIR, ./templates/); class Stupid { 开始写个构造器吧>>> public function Stupid() { 在构造器中,我们判断了模板路径和编译路径是否设置正确. 设计我们的方法 public function assign($var, $value) { display()方法 代码如下>>> public function display($tpl_file) { 这个方法是根据!file_exists($parsed_file)||filemtime($parsed_file)
debug()方法 代码如下>>> public function debug ($tpl_file) {
Stupid模板引擎是由3个文件组成,他们分别是:stupid.class.php,stupid_parser.class.php,stupid_debugger.class.php。
Stupid.class.php的任务是设置变量,模板路径,和显示等功能,而stupid_parser.class.php就是编译模板文件的,stupid_debugger.class.php是用来调试用的。
1.新建一个PHP文件名为:stupid.class.php。
我们的类叫Stupid,我们先设计一下成员变量吧。
成员变量有:$_tpl_vars, $_tpl_file, $_parser, $_debugger;
$_tpl_vars: 用来保存模板变量的;
$_tpl_file: 用来保存模板文件名的;
$_parser: 保存StupidParser对象的,就是编译对象;
$_debugger: 保存StupidDebug对象的,就是调试对象;
define(TPL_DIR, ./templates/);
define(TPL_C_DIR, ./templates_c/);
define(TPL_C_DIR, ./templates_c/);
private $_tpl_vars;
private $_tpl_file;
private $_parser;
private $_debugger;
}
?>
if(!is_dir(TPL_DIR) || !is_dir(TPL_C_DIR)) {
exit(错误:请正确设置模板文件夹和编译文件夹);
}
}
我们这个类中主要有以下方法:
assign(), set_tpl_dir(), set_parsed_dir(), display(), debug().
assign()方法:
assign()的用处是设置模板变量.代码如下>>>
if(isset($var) && trim($var) != ) {
$this->_tpl_vars[$var] = $value;
return true;
} else {
exit(错误:请设置变量名);
}
}
我们先判断用户是否设置了变量名,用isset($var) && trim($var) != 来判断, trim($var) != 是防止用户以空格来设置变量名.如果设置变量正确,我们就将他保存到成员变量_tpl_vars中.
display()方法是Stupid类中最重要的方法,他是用来显示和检测模板是否更新了,更新了就再编译,没有更新就用原来编译之后的文件.
$template_file = TPL_DIR.$tpl_file;
if(!file_exists($template_file)) {
exit(错误:模板文件不存在);
}
$parsed_file = TPL_C_DIR.md5($tpl_file)..php;
if(!file_exists($parsed_file) || filemtime($parsed_file) require_once ./stupid_parser.class.php;
$this->_parser = new StupidParser();
$this->_parser->compile($tpl_file);
}
include $parsed_file;
}
Debugg()方法就比较简单,就是引入stupid_debugger.class.php文件,创建StupidDebuger对象,调用StupidDebuger的start方法进行调试.
if (include_once("stupid_debugger.class.php")) {
$this->_debugger = new StupidDebugger(TPL_DIR. $tpl_file);
$ this-& gt; _debugger-& gt; start ();
} Else {
EXIT (error: Debuger file does not exist);
At this point, our Stupid class is finished! Next time I will introduce the writing of the StupidParser class. Please continue to support. If you have any comments or suggestions, please feel free to put forward!
class Stupid {
private $_tpl_vars;
private $_tpl_file;
private $_parser;
private $_debug;
public function Stupid() {
if(!is_dir(TPL_DIR) || !is_dir(TPL_C_DIR)) {
}
}
public function assign($var, $value) {
if(isset($var) && trim($var) != ) {
return true;
} else {
exit(Error: Please set variable name);
}
}
public function display($tpl_file) {
$template_file = TPL_DIR.$tpl_file;
exit(Error: template file does not exist);
}
$parsed_file = TPL_C_DIR.md5($tpl_file)..php;
if(!file_exists($parsed_file) || filemtime($parsed_file) require_once ./stupid_parser.class.php;
$this->_parser = new StupidParser();
$this->_parser->compile($tpl_file);
}
include $parsed_file;
}
function debug($tpl_file) {
if (include_once("stupid_debugger.class.php")) {
$this->_debugger->start();
} else {
exit(Error: Debuger class file does not exist);
}
}
}
?>

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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.

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