【发布时间】:2013-08-03 06:33:17
【问题描述】:
在我的 UsersController 的索引操作中,我想将字符串和数组传递给视图,以便我的视图正确呈现。我不知道我做错了什么,但我的视图文件没有让我的数组之一查看。
$users = $this->User->getUsers();
//Setting all variable that are present in layout
$this->set('title_for_layout','Users - Admin');
$page_headings = array("text" => "Users", "clss" => "icon-head");
$this->set('page_heading', $page_headings);
//Ends here
//This is actually going to action view file
$this->set(compact('users'));
我的查看文件代码是这样的
<div><?php echo $page_heading[0]['text']; ?></div>
<?php foreach($users as $user){ ?>
我可以在这里获得 $users 变量,但注意到获得了 $page_heading。
我收到此错误Undefined variable: page_heading。
我已经尝试过这样的一切:
$this->set($page_headings);
$this->set(compact('page_headings');
是的,在执行上述代码后,我已将视图文件中的变量名也更改为 page_headings。我无法让它工作。可以请任何人帮忙。
提前致谢。
【问题讨论】:
-
而且
title_for_layout也可以正常提交吗?应该没什么区别,但是使用单个集合调用$this->set(compact('title_for_layout', 'page_heading', 'users'));怎么样? -
ya..
title_for_layout即将到来。我已经尝试将数组变量放在一起$this->set(compact('page_heading','users'));.. 现在我正在尝试您的建议.. 还有一件事我的$page_headings在布局文件,不在动作的视图文件中..这有什么影响..?? -
@ndm 对不起,我对
title_for_layout说错了。它也没有正确显示..在尝试了您建议的代码后,它显示 users 和 page_heading 都是未定义的,也没有显示页面标题.. -
这听起来很奇怪...您的确切 CakePHP 版本是什么?你在布局/视图渲染方面做了什么特别的事情吗?可能是自定义视图类之类的东西?您是否尝试过使用全新的默认 CakePHP 安装是否会出现问题?
-
我正在开发 1.3.17 版本的 cakePhp。不,我没有做任何特别的事情。也许我应该尝试全新安装。
标签: cakephp cakephp-1.3