我想在我的刀片模板中显示嵌套列表。 如果我想快速显示嵌套列表,我必须通过递归来完成。我在我的模板函数中创建了名为renderNode()的函数。但是我的模板中的这个全局函数并不是一个好主意和实践。我想重组它。
您是否了解良好做法,如何整理代码?
<?php
function renderNode($node) {
echo "<li class='dd-item dd3-item' data-id='$node->id'>";
echo "<div class='dd-handle dd3-handle'>Drag</div>
<div class='dd3-content'>
$node->name
<span class='pull-right categories-actions'>
<i class='fa fa-plus-square-o delete-category' title=''></i>
<i class='fa fa-cogs delete-category' title=''></i>
<i class='fa fa-trash-o delete-category' title=''></i>
</span>
</div>";
if ( $node->children()->count() > 0 ) {
echo "<ol class='dd-list'>";
foreach($node->children as $child) renderNode($child);
echo "</ol>";
}
echo "</li>";
}
?>
<div id="content" class="col-lg-10 col-sm-11">
<div class="row">
<div class="col-lg-12">
<div>
<button id="add-new-category" class="btn btn- primary">@lang('categories.new_category')</button>
</div>
<div class="dd" id="nestable3">
<ol class="dd-list">
@if(isset($categories))
@foreach($categories as $category)
<?php renderNode($category); ?>
@endforeach
@endif
</ol>
</div>
</div><!--/col-->
</div><!--/row-->
答案 0 :(得分:0)
你说得对,这不是好习惯。最好的办法是将此代码放在您选择的模型中。模型&gt;类别将是最合适的imo。只需在你的类别模型中创建一个公共函数......
public function renderNode($node)
{
// place code here
return something here;
}
然后调用刀片模板中的模型函数
<?php Category::renderNode( $node ) ?>
您可以使用with。
在路线中通过Make :: view传递$ node但,您可能需要考虑不同的方法。像这样......
@if ( $node->children()->count() > 0 )
<ol class='dd-list'>
@foreach ($node->children as $child)
{{ Category::renderNode($child) }}
@endforeach
</ol>
@endif
答案 1 :(得分:0)
<ol class="dd-list">
@foreach($categories as $category)
<li class='dd-item dd3-item' data-id="$category->id">
<div class='dd-handle dd3-handle'>Drag</div>
<div class='dd3-content'>
{{ $node->name }}
<span class='pull-right categories-actions'>
<i class='fa fa-plus-square-o delete-category' title=''></i>
<i class='fa fa-cogs delete-category' title=''></i>
<i class='fa fa-trash-o delete-category' title=''></i>
</span>
</div>
@if ( $node->children()->count() > 0 )
@foreach ($node->children as $child)
<li class='dd-item dd3-item' data-id='$child->id'>
<div class='dd-handle dd3-handle'>Drag</div>
<div class='dd3-content'>
{{ $child->name }}
<span class='pull-right categories-actions'>
<i class='fa fa-plus-square-o delete-category' title=''></i>
<i class='fa fa-cogs delete-category' title=''></i>
<i class='fa fa-trash-o delete-category' title=''></i>
</span>
@endforeach
@endif
</div>
</li>
@endforeach
</ol>
类似这样的事情