Reputation: 564
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. :(
Upvotes: 2
Views: 800
Reputation: 1421
There is a automatic tool for get this job done. check out PHP2Blade
you can convert multiple files to blade syntax by runnig this command.
php php2blade <files directory> <optional output directory>
Upvotes: 1
Reputation: 2018
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>
Upvotes: 1