Table of Contents
沈逸老师PHP魔鬼特训笔记(8)
创建模板文件:
这节课老师带领我们开始创建TEMPLATE,按照老师教导的思路,我们希望这样一种代码写法:1、譬如我定义一个变量$name=‘’;2、然后呢我读取一个模板。3、再然后我在这个模板里 设置一些自己的 “显示格式”(逼格)。4、加载模板后可以直接替换成上面的变量。
一、首先在template文件夹中新建一个index.tpl。内容这么写
<?php <span style="color: #0000ff;">echo '<?php ' ?> <span style="color: #008000;">/*</span><span style="color: #008000;">* * project name: <?php echo $prj_name ?> //大家想想, 它能运行吗? *User: <?php echo $prj_author ?> //能运行吗? *Date: <?php echo date('Y-m-d')?> //能吗? </span><span style="color: #008000;">*/</span> <span style="color: #0000ff;">echo</span> "hello shenyi"<span style="color: #000000;">; </span>?>
Copy after login
好吧,准备工作就做好了,我们在god_frame这个类中的引用它:
<span style="color: #000000;">php namespace core\frame; </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> god_frame { </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$project_folder</span> = ''; <span style="color: #008000;">//</span><span style="color: #008000;">项目文件夹</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$project_main</span> = ''; <span style="color: #008000;">//</span><span style="color: #008000;">入口文件</span> <span style="color: #0000ff;">function</span> __construct(<span style="color: #800080;">$prjName</span>){ <span style="color: #008000;">//</span><span style="color: #008000;">构造函数</span> <span style="color: #800080;">$this</span>->project_folder = <span style="color: #008080;">getcwd</span>()."/".<span style="color: #800080;">$prjName</span><span style="color: #000000;">; </span><span style="color: #800080;">$this</span>->project_main = <span style="color: #800080;">$this</span> -> project_folder."/index.php"<span style="color: #000000;">; } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> run(){ </span><span style="color: #008000;">//</span><span style="color: #008000;">判断并生成新的文件夹,没有就创建</span> !<span style="color: #008080;">file_exists</span>(<span style="color: #800080;">$this</span>->project_folder) && <span style="color: #008080;">mkdir</span>(<span style="color: #800080;">$this</span>-><span style="color: #000000;">project_folder); //获取外部成员变量并把该函数获取的数组返回成变量列表 </span><span style="color: #008080;">extract</span>(<span style="color: #008080;">get_object_vars</span>(<span style="color: #800080;">$this</span><span style="color: #000000;">)); 开启PHP的内部缓冲区(内存) </span><span style="color: #008080;">ob_start</span><span style="color: #000000;">(); </span><span style="color: #008000;">//</span><span style="color: #008000;">引入模板路径</span> <span style="color: #0000ff;">include</span> (<span style="color: #008080;">dirname</span>(<span style="color: #ff00ff;">__FILE__</span>).'/template/index.tpl'<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;">获取缓冲区的内容,并赋给$cnt</span> <span style="color: #800080;">$cnt</span> =<span style="color: #008080;">ob_get_contents</span><span style="color: #000000;">(); </span><span style="color: #008000;">//</span><span style="color: #008000;">清理缓冲区内容</span> <span style="color: #008080;">ob_end_clean</span><span style="color: #000000;">(); </span><span style="color: #008000;">//</span><span style="color: #008000;">在该文件夹下生成一个index.php文件,没有就创建并覆盖</span> <span style="color: #008080;">file_put_contents</span>(<span style="color: #800080;">$this</span>->project_main,"<span style="color: #800080;">$cnt</span>"<span style="color: #000000;">); } } </span>?>
Copy after login
我还需要改进godinit中的start(方法)
<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> start(){ </span><span style="color: #800080;">$get_config</span> =<span style="color: #000000;"> loadConfig(); </span><span style="color: #800080;">$gf</span> = <span style="color: #0000ff;">new</span> god_frame(<span style="color: #800080;">$get_config</span>-><span style="color: #000000;">prj_name); </span><span style="color: #800080;">$gf</span> -> prj_name = <span style="color: #800080;">$get_config</span>-><span style="color: #000000;">prj_name; </span><span style="color: #800080;">$gf</span> -> prj_author = <span style="color: #800080;">$get_config</span>-><span style="color: #000000;">prj_author; </span><span style="color: #800080;">$gf</span> -><span style="color: #000000;"> run(); }</span>
Copy after login
在命令行中执行这个方法
然后我们看看整个文档的目录结构以及index.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
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
Roblox: Grow A Garden - Complete Mutation Guide
3 weeks ago
By DDD
How to fix KB5055612 fails to install in Windows 10?
3 weeks ago
By DDD
Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago
By 尊渡假赌尊渡假赌尊渡假赌

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
Java Tutorial
1666
14


CakePHP Tutorial
1426
52


Laravel Tutorial
1328
25


PHP Tutorial
1273
29


C# Tutorial
1255
24

