2016-04-15 64 views
0

我正在学习Kohana 3.3使用来自Internet的非常简单和基本的示例。Kohana 3.3查看foreach不输出数组

我的控制器:

类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浏览器输出两个标题:在线商店和产品目录确定。但在foreach圈所在的位置输出

$cost): ?> 

我在做什么错?我无法循环访问这个数组吗?或者,也许我的语法错了?将不胜感激,以帮助我的错误。

回答

1

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

<? ?> 
0

看来,在这个代码,最好使用完整的语法

<?php ?> 

而不是

<? ?> 

我试图将代码更改为:

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

而现在工作得很好。