在拉拉维尔中从模板移动函数的位置


Where move functions from template in Laravel

我想在我的刀片模板中显示嵌套列表。如果我想快速显示嵌套列表,我必须通过递归来完成。我在名为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-->

你是对的,这不是好的做法。 最好的办法是将此代码放在您选择的模型中。 类别>模型将是最合适的 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
<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>

像这样的东西