【问题标题】:Dwoo / codeigniter: Is there a way to assign data from php to dwoo vars INSIDE templates?Dwoo / codeigniter:有没有办法将数据从 php 分配给 dwoo vars INSIDE 模板?
【发布时间】:2010-08-03 22:31:16
【问题描述】:

纯 dwoo 等效项是:

$dwoo->output('DwooTest/index', array('assignedVar' => 'Hello'));

(我实际上将它与 codeigniter 一起使用 - 与 Phil Sturgeon 的库一起使用):

$this->dwooParser->parse('DwooTest/index', array('assignedVar' => 'Hello'));

然后在 index.php 中

{$assignedVar} //outputs 'Hello'

<?php
    $localVar = 'LocalVar';
?>

{$localVar}  //output: error

有没有办法将模板内的 php 中的数据传递给 dwoo var ?

为什么我使用它是因为我有一个需要某种预处理的视图(它有点像高级视图,所以我不想每次都将处理放在控制器中),在 index.php 里面我有一个

require 'index.h.php' //(notation inspired from c++ header files)

按照上面的例子,index.h.php 会处理 $assignedVar,并将数据放入 $localVar,然后数据的显示将在模板 index.php 中进行。

(顺便说一句,这个 Dwoo 的文档在哪里...我的意思是那个 wiki...就是它?)

【问题讨论】:

    标签: php dwoo


    【解决方案1】:

    在执行模板时,局部变量被保存到 Dwoo 对象的内部变量中。实际的模板代码在 Dwoo 对象的上下文中执行,因此您可以使用 $this 从 php 访问其方法。

    在这种情况下,您想要的方法是 assignInScope($val, $scope),它将分配它,例如:

    <?php $this->assignInScope('Hello', 'localVar'); ?>
    {$localVar} // outputs Hello
    

    您也可以使用readVar($name) 读取它,即:

    <?php echo $this->readVar('localVar'); ?> // outputs Hello again
    

    【讨论】:

      猜你喜欢
      • 2011-05-30
      • 2011-01-02
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      • 2023-01-01
      • 2012-04-24
      • 1970-01-01
      相关资源
      最近更新 更多