这节课老师带领我们开始创建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>?>
好吧,准备工作就做好了,我们在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>?>
我还需要改进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>
在命令行中执行这个方法
然后我们看看整个文档的目录结构以及index.php 的内容