Templates Codeigniter始终加载在控制器中

Templates Codeigniter始终加载在控制器中,templates,codeigniter,Templates,Codeigniter,我来自俄罗斯,很抱歉英语不好。 我想在控制器的每个页面中加载模板。 例如(自动加载中的库解析器) header.php: <h1>{header}</h1> <script> alert("Welcome!"); </script> {header} 如何在每页中加载页眉?谢谢。尝试使用$this->header调用数组加载类属性时,如上面的$header,php希望在变量名之前声明属性的可见性。PHP5有三个可见性选项:“公共”、“私有”或“

我来自俄罗斯,很抱歉英语不好。 我想在控制器的每个页面中加载模板。 例如(自动加载中的库解析器)

header.php:

<h1>{header}</h1>
<script>
alert("Welcome!");
</script>
{header}

如何在每页中加载页眉?谢谢。

尝试使用$this->header调用数组

加载类属性时,如上面的
$header
,php希望在变量名之前声明属性的可见性。PHP5有三个可见性选项:“公共”、“私有”或“受保护”。我认为这就是为什么会出现“意外的T_变量”。它们之间的区别如下所述:

为了防止易访问性污染,PHPV5引入了3个前缀来声明类方法或变量:public、protected和private

公共方法和变量可以在类之外访问。受保护的类只能从类内部和继承类或父类访问。只能从类本身内部访问私有

尝试以下方法:(我选择了“公共”可见性,您可以确定哪一个适合您的使用)

接下来,我认为应该在构造函数中调用解析器,而不是在类方法之外

function _construct(){
    parent::_construct();
    $this->parser->parse('header',$this->header);
}
每次实例化该类时都会调用构造函数,同时加载解析器库方法

更新:

您的评论表明解析器没有像您预期的那样工作。我想你已经

$this->parser->parse('header,$this->header)

在构造函数中,就像我建议的那样。如果这不起作用,创建一个与类同名的函数,并将解析器放在其中,每次调用类时都会加载该函数,类似于构造函数,但让我们看看这是否起作用。我建议在解决问题之前,不要自动加载解析器库,只是为了简化工作

function blog(){
    $this->load->library('parser');
    $this->parser->parse('header',$this->header);
}

可能是键入错误,但在header.php的代码中键入了
{header}
,我猜应该是
{$header}

config.php:

$autoload['libraries'] = array('parser');
controller blog.php:

<?php

class Blog extends Controller {

function __construct() 
    {
            parent::Controller();
            $header = array('header' => 'Welcome to my blog!');
            $this->parser->parse('header', $header);
    }

function index()
    {
        echo "Мой текст";
    }


}
?>

它对我有效。

没有错误,已加载页面,但未加载视图。只有“我的文本”没有标题。
$autoload['libraries'] = array('parser');
<?php

class Blog extends Controller {

function __construct() 
    {
            parent::Controller();
            $header = array('header' => 'Welcome to my blog!');
            $this->parser->parse('header', $header);
    }

function index()
    {
        echo "Мой текст";
    }


}
?>
{header}