【问题标题】:Class include not letting variables work inside templates类包括不让变量在模板中工作
【发布时间】:2014-10-15 18:10:48
【问题描述】:

我有一个 PHP 类,可以更轻松地插入我创建的模板...

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

它位于public_html之外的目录中

kms/php_includes/kms.php

但如果我在我的索引页面中使用它

public_html/index.php

我在索引中设置的变量在模板中都不起作用?

例子

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

但是在kms/site/header.php

我将undefined variable user 定向到头php 文件。这是因为模板的来源不在public_html 之外,还是我在这里做了一些愚蠢的事情而没有意识到。

在我看来,问题在于文件的位置在完全不同的目录中,所以我的简单做法是添加一个集合并获取。

 ...
 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 "";
       }
    }

【问题讨论】:

    标签: php class include undefined


    【解决方案1】:

    这两个文件无法相互通信,因为它们被赋予了两个不同的范围;它们包含在对template()的两个单独调用中

    尝试在index.php 中设置$controller->user 并在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
    

    您还可以将变量存储在数组中并在包含标题之前提取它们。

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

    我建议不要这样做,但是,如果您的模板很大并创建自己的模板,您很容易忘记变量的来源。但如果需要,您可以将提取的变量名称全部大写,以表明它们来自控制器。

    $this->templateVars['USER'] = 'Foo'; // etc
    

    【讨论】:

    • $controller$controller = new KMS(); 那么为什么 $controller->user 会起作用?我没试过,只是好奇。还有一个调用是从索引页面到我的模板标题。我以前让它们工作过一次,但是模板、类以及我正在执行此操作的位置(即管理页面)都在同一个父目录中。我认为这可能与每个文件的位置有关。
    • 是的,这就是我在回答时所建议的。 :) 问题不在于文件位于不同的目录中,问题在于您创建的变量具有不同的范围并且无法看到彼此。通过将变量附加到控制器,您可以为模板提供共享的可见性范围。
    • 好的,那么我将如何使用上面的 foreach 示例reassign 这些变量?我尝试使用$$,但没有按计划工作。
    • 你不需要。只需在模板中使用 $this->get() 即可。
    • 如果你真的想把它们拉出来,试试 extract()
    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    相关资源
    最近更新 更多