具有2D数组的CodeIgniter模板库 [英] CodeIgniter Template Library with a 2D array
问题描述
我正在使用以下模板库.我想做的是将一个或多个上方视图作为数组加载到模板中,这样我就可以通过for-each循环轻松地将它们加载到模板中.
I am using the following template library. What I am trying to do is to load one or more upper views inside a template,as an array so that I can easily load them in the template with a for-each loop.
这是一个关于如何在控制器上使用它的简单示例:
This is a simple example on how it can be used on a controller:
function index() {
$data['title'] = 'My page title';
$partials = array('content'=>'c_location'); //Load view about inside the template.
$this->template->load('main', $partials, $data);
}
在视图上,您有一个类似html的
On the View you have an html like:
<html>
....
<?=$content?>
...
</html?>
这就是我要使用的: 控制器:
This is what I am trying to use: Controller:
$partials = array('content'=>'c_location',
array(
'first_upper_content'=>'1_u_location','second_upper_content'=>'2_u_location'
)
);
例如,我可以传递upper_content,顶部标头传递为"first_upper_content",幻灯片传递"second_upper_content",然后将其余的内容传递给"content".
So for example I could pass for upper_content, a top header as "first_upper_content" and a slide for "second_upper_content" and then the remainder of the content for "content".
HTML:
...
<?=$upper_content?>
<--if upper_content is a array,
I could display each content with a for loop-->
<?=$content?>
当我尝试一切时,我会得到:
When I try everything I get:
消息:pathinfo()期望参数1为字符串,给定数组
Message: pathinfo() expects parameter 1 to be string, array given
文件名:core/Loader.php
Filename: core/Loader.php
行号:759
我该如何实现?我正在考虑修改
How can I implement this? I am thinking of modifying the
///将视图加载到var数组
// Load views into var array
Template.php&内部在HTML中添加一个foreach循环;
Inside the Template.php & adding a foreach loop to the html;
推荐答案
这就是您想要的:
// Load views into var array
foreach(array_flat($view) as $key => $file)
如上面所示,在Template.php中包括" array_flat "函数调用.
Include the "array_flat" function call in Template.php, as shown above.
您将需要定义此功能.您可以在任何自动加载的帮助器中执行此操作,甚至可以将其包含在自己的Template类中(在这种情况下,您应该在上面的代码中将其称为$this->array_flat
).就是这样:
You will need to define this function. You can do this in any auto-loaded helper, or even include it the own Template class (in this case, you should call it as $this->array_flat
in the code above). Here is it:
function array_flat($array)
{
$result = array();
foreach ($array as $key => $value)
{
if (is_array($value))
{
$result = array_merge($result, array_flat($value));
}
else
{
$result[$key] = $value;
}
}
return $result;
}
这篇关于具有2D数组的CodeIgniter模板库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!