如何在Smarty中通过递归函数创建一个数组变量 [英] how to foreach an array variable created with recursive function in Smarty
问题描述
我有一个数组,我用PHP创建了一个递归函数,我不知道有多少维度,我该如何在Smarty中使用?
此代码:
{foreach $ myArr as $ items}
< li>
{$ items.title}
{if $ item.submenu}
< ul>
{foreach $ items.submenu as $ items2}
< li> {$ items2.title}< / li>
{/ foreach}
< / ul>
{/ if}
< / li>
{/ foreach}
但是这个代码只有2个级别,可能是我的数组有3个或4个或...级别。
更新:
找到了解决方案,在我的解决方案中,我使用了Smarty函数:
$ p $ {code $ {function name = menu level = 0}
< b ; UL>
{foreach $ data as $ items}
< li>
< a href ={$ items.url}>
{$ items.title}
< / a>
{if is_array($ items.submenu)}
{menu data = $ items.submenu level = $ level + 1}
{/ if}
< / li>
{/ foreach}
< / ul>
{/ function}
{menu data = $ menuItems}
我会采取不同的方法。
我的建议:
而不是用菜单创建数组。创建两个或一个class / es。
第一类是一个Menu类,以键/值方式保存项目。
第二类将是一个菜单项。
这样,所有必要的迭代/逻辑都可以通过模型
中的函数调用来完成。打印在视图中。
类似于:
class Menu {
保护$ _items = array();
protected $ _parent_child = array();
public function add_menu_item($ name,$ id,$ parent_id){
if(array_key_exists($ id,$ this-> _items))
return;
$ b $ this-> _items [$ i] = array('name'=> $ name,'id'=> $ id,'parent'=> $ parent_id)//带有数据的模型
$ this-> _parent_child [$ parent_id] = $ this-> _items [$ id];
$ b public function get_nodes_by_parent($ id = null / * for root * /){
if(array_key_exists($ id,$ this-> _parent_child ))
return $ this-> _parent_child [$ id];
return array(); //或null或
}
$ b}
菜单模板:
{foreach($ menuClass-> get_nodes_by_parent()as $ item){
< ul>
< li> {$ item ['name']}< / li>
{include'sub_cats'id = $ item ['id'],menuclass = $ menuclass}
< / ul>
{endforeach}
子类别模板:
{foreach($ menuClass-> get_nodes_by_parent($ id)as $ item){
< ul>
< li> {$ item ['name']}< / li>
{include'sub_cats'id = $ item ['id']}
< / ul>
{endforeach}
这并不完整,也没有经过测试。但那是我的做法。
I have an array which I created in php with a recursive function, I do not know how many dimensions, how can I use in Smarty ?
I trying use this code :
{foreach $myArr as $items}
<li>
{$items.title}
{if $item.submenu}
<ul>
{foreach $items.submenu as $items2}
<li>{$items2.title}</li>
{/foreach}
</ul>
{/if}
</li>
{/foreach}
But this code is for just 2 levels, may be my array have 3 or 4 or ... levels.
UPDATE:
I found the solution, in my solution I use Smarty functions :
{function name=menu level=0}
<ul>
{foreach $data as $items}
<li>
<a href="{$items.url}">
{$items.title}
</a>
{if is_array($items.submenu)}
{menu data=$items.submenu level=$level+1}
{/if}
</li>
{/foreach}
</ul>
{/function}
{menu data=$menuItems}
I would go a different approach. My suggestion: Instead of creating arrays with menus. Create two or one class/es.
First class is a Menu class which holds the items in a key/value manner. Second class would be a menu item.
That way you could all necessary iterations/logic do through a function call in the model and the printing in the view.
something like:
class Menu {
protected $_items = array();
protected $_parent_child = array();
public function add_menu_item($name, $id, $parent_id) {
if (array_key_exists($id, $this->_items))
return;
$this->_items[$i] = array('name' => $name, 'id' => $id, 'parent' => $parent_id) // model with data
$this->_parent_child[$parent_id] = $this->_items[$id];
}
public function get_nodes_by_parent($id=null /*for root*/) {
if (array_key_exists($id, $this->_parent_child))
return $this->_parent_child[$id];
return array(); // or null or something
}
}
Menu template:
{foreach($menuClass->get_nodes_by_parent() as $item ) {
<ul>
<li>{$item['name']}</li>
{include 'sub_cats' id=$item['id'], menuclass=$menuclass}
</ul>
{endforeach}
Sub category template:
{foreach($menuClass->get_nodes_by_parent($id) as $item ) {
<ul>
<li>{$item['name']}</li>
{include 'sub_cats' id=$item['id']}
</ul>
{endforeach}
This is not fully complete nor tested. But that would be my approach.
这篇关于如何在Smarty中通过递归函数创建一个数组变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!