【问题标题】:Passing data to views in Phil Sturgeon Codeigniter template library将数据传递给 Phil Sturgeon Codeigniter 模板库中的视图
【发布时间】:2016-05-30 22:48:37
【问题描述】:

已安装Phil Sturgeon Codeigniter Template library

似乎库安装正确,因为我可以使用title()、set_partial() 和set_layout 设置标题。一切正常。但我不能传递任何数据。

如果我在 Controller 中使用 $this->template->title('My Title'); 设置新标题 我可以使用echo $template['title']在任何视图中轻松获得标题

例如,我想传递一条消息:
在视图中:

<?php echo $template['message']; ?>

在控制器中我尝试:

$this->template->set('message', 'My new mess');
$this->template->build('dashboard');
// I get *Undefined index: message* from View


$data['message'] = ['My new mess'];
$this->template->build('dashboard', $data);
// Same *Undefined index: message*


$this->template->build('dashboard', array('message' => 'My new mess'));
// Even this does not work


使用 CI 3。 关于解析器的模板配置设置:

$config['parser_enabled'] = FALSE;
$config['parser_body_enabled'] = FALSE;

好像漏掉了一个小细节。

【问题讨论】:

    标签: php codeigniter templates codeigniter-3


    【解决方案1】:

    我终于明白了。

    我自己的主要困惑来自文档本身。 只有模板设置的标题为$this-&gt;template-&gt;title(),才能在视图中调用为$template['title']。

    所有其他数据按如下方式传递

    在控制器中:

    $this->template->set('message', 'My message');
    $this->template->build('dashboard');
    

    仅在视图中:

    echo $message
    

    【讨论】:

    • 注意 5 年前的代码并仔细检查其中的所有方法。
    • 到目前为止,CI 3.0.6 没有问题。实际上,我很惊讶这可能是最受欢迎的模板库 4 年没有任何更新。虽然我没有尝试使用 CI 解析器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 2013-03-14
    • 2011-02-20
    相关资源
    最近更新 更多