douchai7891 2014-10-15 18:10
浏览 34
已采纳

类包括不让变量在模板中工作

I have a PHP class that makes it just easier to insert templates that I've created...

ie

...
 /*Get Templates*/
 public function template($file){
       if(isset($file) && file_exists($file.".php")){
          $controller = $this;
          include $file.".php";
       }else{
          echo "";
       }
    }

Which is located in a directory outside public_html as so

kms/php_includes/kms.php

But if I am using it in my index page

public_html/index.php

None of the variables i set in index work inside the templates?

Example

index.php

$user = "Mr.EasyBB";
$controller->template("../kms/site/header");//$controller is the class variable

header.php

echo $user; //this doesn't work
$controller->template("../kms/site/svg/smile"); //this still works

But in kms/site/header.php

I get the undefined variable user directed to the header php file. Is this due to the origin of the template being outside of public_html or am I doing something silly here and not realizing it.

Seems to me the problem is the location of the files being in complete different directories so my trivial practice was to add a set and get.

 ...
 private $variables = array();

 public function get($prop){
    if($this->variables[$prop]){
       return $this->variables[$prop];
    }
 }
 public function set($prop=null,$val=null){
   if($prop !== null && $val !== null){
       $this->variables[$prop] = $val;
     }
 }

 /*Get Templates*/
 public function template($file){
       if(isset($file) && file_exists($file.".php")){
          $controller = $this;
          foreach($this->variables as $var->$val){
             //i need to assign explicitly the $var name here hence why I used double dollar signs. But it's not working either.
             $$var = $val;
          }
          include $file.".php";
       }else{
          echo "";
       }
    }
  • 写回答

1条回答 默认 最新

  • dpnfjx755573 2014-10-15 18:26
    关注

    Those two files cannot communicate with each other because they are given two different scopes; they are included in two separate calls to template()

    Try setting $controller->user within index.php and reusing it in header.php.

    // controller
    function template() {
        $this->user = 'Foo';
        include 'header.php'; // tells php to use header.php as if
                              // it were just more code for this function
    }
    
    // header.php
    echo $this->user; // Foo
    

    You can also stash your variables in an array and extract them before including the header.

    // controller
    function template() {
        $this->templateVars['user'] = 'Foo';
        extract($this->templateVars);
        echo $user; // Foo
        include 'header.php';
    }
    
    // header.php
    echo $user; // Foo
    

    I would advise against that, however, you can forget where variables come from easily if your templates are large and create their own. But if you need to, you can make the extracted variable names all uppercase to indicate that they came from the controller.

    $this->templateVars['USER'] = 'Foo'; // etc
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.