Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

metor blade template throws "second landmark in same branch" exception in for loop

I have a for loop that I want to include a template view in:

table.blade:

#game
    h1= table.name 
        -if( table.playerSitting() )
            a.btn.btn-danger.stand(href="#") Stand
    #table
        -for (var i=0; i<7;i++)
            include 'seat' exposing i

seat.blade:

- id = 'p' + (i+1)
    div(id="#{id}")
        -if (table.hasAt(i))
            p= table.usernameAt(i)
        -else
            -if( !table.playerSitting() )
                a.btn.btn-inverse.sit(href="#", data-key="#{i}") Sit
            -else
                p &nbsp;

When I first load the view, everything draws fine. But if I sit or stand, or go to another table, I get this:

Exception from Deps recompute: Error: Can't create second landmark in same branch
    at /home/thomporter/meteor/blackjack/views/table.blade:8:4

 6 |        #table
 7 |            -for (var i=0; i<7;i++)
 8 >                include 'seat' exposing i
 9 |    
10 |    

I figure I can fix this by creating a "seats" collection that stores the user id & table ids, but that seems silly. I'm currently using an array property of the table called "players", index 0-6 for each seat... at least, that's the idea. =)

I should also mention I replaced seat.blade with:

p hi

and no luck, same issues.

Any ideas?

Edit
Version Information:
Meteorite version 0.4.9
Meteor version 0.5.9 (git checkout)
Blade 3.0.3

like image 382
Thom Porter Avatar asked Oct 22 '22 14:10

Thom Porter


1 Answers

This bug is actually a result of using plain-old for loops instead of foreach loops.

The foreach region is preferred over ... [regular for loops] not only because of readability and brevity, but because it also provides Blade with the ability to better integrate with live page updating engines (specifically Meteor and Spark). That is, if the live page update engine supports tracking reactive collections, the most efficient DOM operations may occur to update the view's results in-place, without re-rendering the entire Blade template.

In your case, Spark is failing to re-render your included templates because of matching branches for the same landmark. If you use foreach, this problem is resolved.

However, in some situations (like yours), foreach is not appropriate. I think that the solution here is to create a "regular for loop" syntax within Blade to handle loops. Because of this, I created issue # 157.

like image 132
BMiner Avatar answered Oct 25 '22 18:10

BMiner