Blogger Information
Blog 21
fans 2
comment 3
visits 43754
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
5.29模板赋值与模板内容过滤与替换技术
李洋
Original
798 people have browsed it
  • 模板赋值


    • 实例

      <?php
          class Index{
                public function demo()
                {
                     $name = "李洋";
                     //模板赋值
                     //使用View类的静态代理 调用assign()即可为模板赋值
                    \think\facade\View::assign("name",$name);  
                    //使用view类的__set()魔术方法赋值  
                    $this -> view -> name = $name;  //调用不存在的变量或者属性 自动调用__set()魔术方法
                    //通过view属性 view属性及时View类的实例化 调用其中的assign()方法
                    $this -> view -> assign("name",$name);
                   //也可使用Controller控制器中的assign()方法
                    $this -> assign("");
                   //也可以使用fetch()方法 传递参数传值
                   return $this -> view -> fetch("");
                  // return \think\facade\View::fetch();
                 }
         }

      运行实例 »

      点击 "运行实例" 按钮查看在线实例

  • 模板过滤和替换

    • 实例

      <?php 
          class Index{
                //模板过滤和替换  这是局部过滤和替换  在config/Template.php中配置的替换是全局替换
              public function demo2()
             {
                $name = "小李";
                //过滤 使用匿名函数进行处理
                $filter = function ($content){
                   return str_replace("小李","李洋",$content);
                };
               $this -> view ->assign("name",$name);
               //return $this -> view -> filter($filter) -> fetch();
               //调用filter()方法 并且把处理的参数传递过去
               return  $this -> filter($filter) -> fetch();
              }
           }

      运行实例 »

      点击 "运行实例" 按钮查看在线实例

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!