【问题标题】:Kohana 3.3 view foreach not outputting arrayKohana 3.3查看foreach不输出数组
【发布时间】:2016-04-15 07:34:28
【问题描述】:

我正在使用来自 Internet 的非常简单和基本的示例来研究 Kohana 3.3。

我的控制器:

类 Controller_Index 扩展 Controller_Template {

public $template='v_index';

public function action_index()
{

    $this->template->title='Online store';
    $this->template->content='Homepage';
}

public function action_catalog()
{
    $title='Products catalog';
    $products = array(
        'Product 1'=>100,
        'Product 2'=>200,
    );

    $this->template->title='Online products store';

    $this->template->content=View::factory('v_catalog')
        ->bind('products',$products)
        ->bind('product',$product)
        ->bind('cost',$cost)
        ->bind('title',$title);
}

}

我的观点 v_index.php

 <h1><?=$title;?></h1>
 <hr>
 <p><?=$content;?></p>

我的观点v_catalog.php:

 <h2><?=$title?></h2>

 <? foreach ($products as $product=>$cost): ?>
     <p><?=$product?><strong><?=$cost?></strong></p>
 <? endforeach; ?>

当我转到http://localhost/kohana/index/catalog 时,浏览器会输出两个标题:在线商店和产品目录 ok。但是在 foreach 圆圈所在的地方,它会输出

 $cost): ?> 

我做错了什么?我不能遍历这个数组吗?或者我的语法错误?希望能帮助我解决我的错误。

【问题讨论】:

    标签: kohana kohana-3.3


    【解决方案1】:

    这是因为 PHP 中的 short_open_tag 选项被禁用。 Here 您有详细信息如何启用此选项。之后你可以使用:

    <? ?>
    

    【讨论】:

      【解决方案2】:

      似乎在这段代码中最好使用完整的语法

       <?php ?>
      

      代替

       <? ?>
      

      我尝试将代码更改为:

       <?php foreach ($products as $product=>$cost): ?>
           <p><?=$product?><strong><?=$cost?></strong></p>
       <?php endforeach; ?>
      

      现在它工作得很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多