> php教程 > php手册 > 沈逸老师PHP魔鬼特训笔记(8)

沈逸老师PHP魔鬼特训笔记(8)

WBOY
풀어 주다: 2016-09-05 08:45:40
원래의
1337명이 탐색했습니다.

创建模板文件:

  这节课老师带领我们开始创建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 的内容

   沈逸老师PHP魔鬼特训笔记(8)

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿