本文介绍了Laravel刀片,将php的递归视图转换为刀片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php中有这个递归视图,这是很明显的错误,因此我必须将其翻译为Blade,以便递归调用视图.

我在(comments.blade.php)中的php递归代码:

 <?phptree($ Comments,0,0);$ var = -1;函数树($ Comments,$ parent_id = 0,$ level = 0,$ c = 0){全局$ var;foreach($ Comments as $ Comment){if($ Comment ['parent_id'] == $ parent_id){如果($ level> $ var)$ var ++;别的 {for($ i = $ var- $ level + 1; $ i> 0; $ i--){if($ c< 0)echo'</div></div>';否则$ c--;};$ var = $ level;};回声'< div class ="media">< div class ="media-left">< a href =#">< img class ="media-object" src ="..." alt ="..."></a></div>< div class ="media-body">< h4 class ="media-heading">媒体标题</h4>';树($ Comments,$ Comment ['id'],$ level + 1,$ c);}};};?> 

这就是我想要的(在comments.blade.php中:

 < div>@include('recursive',['comments'=> $ comments,'parent_id'=> 0,'level'=> 0,'c'=> 0])</div> 

问题是我不知道如何将第一段代码中的所有变量都转换为blade,因此要创建Recursive.blade.php:

伙计们请帮我,我花了2天时间来研究这个该死的算法,而我又陷入了一个比2天前更大的问题:

对于一些不理解为什么我坚持使用该代码的人,是因为上面的代码是用于在reddit上进行线程注释的算法,该算法中有2x echo和media class echo.如果我将媒体课后的2x放在连续2个注释上,则下一个注释将与第一个注释一起创建,注释具有相同的parent_id,这意味着它们是属于同一注释的子对象(在它们之前的父对象),如果2xdiv不放,则意味着第一个之后的下一个评论是第一个的子代,依此类推.这是我的算法,我这样做是为了将其转换为刀片,因为我有一些厚脸皮的投票按钮,该按钮使用的路由将集成到递归设计中,这就是为什么我要转换为刀片的原因,但是我不知道该怎么做.:(

解决方案

为什么要递归调用生成视图,您可以创建文件名comment.blade.php并在主刀片文件中运行一个循环,该循环将包括将comment.blade.php传递给注释对象或数组,然后comment.blade.php文件将使用该$ comment对象或数组的属性.

例如:

  $ Comments = [['body'=>第一条评论",'名称'=>第一评论员"],['body'=>第二条评论",'名称'=>第二评论员"],['body'=>第三条评论",'名称'=>第三评论员"],['body'=>第四条评论",'名称'=>第四评论员],['body'=>第五条评论",'名称'=>第五评论员],['body'=>第六条评论",'名称'=>第六评论员],]; 

以及您的主刀片文件中可以是index.blade.php或其他任何文件

  @foreach($ Comments as $ Comment)@include('comment',['comment'=> $ comment])@endforeach 

comment.blade.php文件将具有

 < div class ="media">< div class ="media-left">< a href =#">< img class ="media-object" src ="..." alt ="..."></a></div>< div class ="media-body">< h4 class ="media-heading">媒体标题</h4></div></div> 

I have this recursive view with php, which is plain wrong so I have to translate this to blade, for recursive call for a view.

My php recursive code in (comments.blade.php):

<?php
tree($Comments, 0, 0);
$var = -1;
function tree($Comments, $parent_id = 0, $level=0, $c=0) {
  global $var;
  foreach($Comments as $Comment) {
      if($Comment['parent_id'] == $parent_id) {
        If ($level > $var)  $var++; else {
                for ($i = $var-$level+1; $i>0; $i--) { if ($c < 0) echo '</div> </div>'; else $c--;  };
                $var=$level;
                   };
        echo '<div class="media">
                <div class="media-left">
                  <a href="#">
                  <img class="media-object" src="..." alt="...">
                  </a>
                </div>
              <div class="media-body">
                <h4 class="media-heading">Media heading</h4>';
        tree($Comments, $Comment['id'], $level+1,$c);
        }
  };
};
?>

And that's what I am looking for(in comments.blade.php:

<div>
    @include('recursive', ['comments' => $comments, 'parent_id' => 0, 'level' => 0, 'c' => 0])
</div>

The thing is I don't know how to translate all that variables in first snippet of code into blade, so to create Recursive.blade.php:

Edit 1:

Guys please help me, I worked 2 days on this damn algorithm and I am stuck once again in a problem even bigger then it was 2 days ago :(

For some people that doesn't understand why I am sticking with that code is because that code above is the algorithm for making the threaded comments like that on reddit, in the algorithm there is 2x echo, and a media class echo.If i put 2x after the media class on 2 comments in a row, the next one comment will create with first one, comments with same parent_id, this means they are childs that belong to the same comment(parent before them), If the 2x div are not put then this means the next one comment after first is the child of the first and so on. This is my algorithm, i am doing this to translate to blade because I have some cheeky votes buttons which uses routes that i will integrate into recursive design, that's why i want to be translate to blade, but i don't know how. :(

解决方案

Why would you call the recursively for generating the view, you can make an file name comment.blade.php and run a loop inside main blade file which will include the comment.blade.php and pass the comment object or array and then the comment.blade.php file will user the properties of that $comment object or array.

For example:

  $Comments = [
    [
      'body' => 'First comment',
      'name' => 'First Commentator'
    ],
    [
      'body' => 'Second comment',
      'name' => 'Second Commentator'
    ],
    [
      'body' => 'Third comment',
      'name' => 'Third Commentator'
    ],
    [
      'body' => 'Fourth comment',
      'name' => 'Fourth Commentator'
    ],
    [
      'body' => 'Fifth comment',
      'name' => 'Fifth Commentator'
    ],
    [
      'body' => 'Sixth comment',
      'name' => 'Sixth Commentator'
    ],
  ];

and in your main blade file that can be index.blade.php or any other

  @foreach($Comments as $Comment)
    @include('comment',['comment'=>$comment])
  @endforeach

comment.blade.php file would have

<div class="media">
    <div class="media-left">
      <a href="#">
      <img class="media-object" src="..." alt="...">
      </a>
    </div>
    <div class="media-body">
      <h4 class="media-heading">Media heading</h4>
    </div>
  </div>

这篇关于Laravel刀片,将php的递归视图转换为刀片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:09