0

Sorry, I'm having a little brain freeze here and am hoping for some help.

I currently have the following code:

<div class="my-div">
    {exp:playa:children channel="somechannel"}

        {matrix_field}

            {if field_row_index == 0}
                <div>
                    <ul>
            {/if}

            {playa_field}                                   

                playa vars

            {/playa_field}

            {if row_count == total_rows}
                        </ul>
                    </div>
            {/if}

        {/matrix_field}

    {/exp:playa:children}

</div>

I'm trying to figure out how to get my-div (opening and closing div) inside the exp:playa:children tag but, to only display once.

In other words, I want my-div to wrap all the playa:children IF and when any exist but, to not repeat over and over again.

Currently what I have works fine except if there isn't any data then my-div still shows up which is not what I want.

Thanks!

1

How about:

    {exp:playa:children channel="somechannel"}
{if count == "1"}<div class="my-div">{/if}

        {matrix_field}

            {if field_row_index == 0}
                <div>
                    <ul>
            {/if}

            {playa_field}                                   

                playa vars

            {/playa_field}

            {if row_count == total_rows}
                        </ul>
                    </div>
            {/if}

        {/matrix_field}

{if count == total_results}</div>{/if}
    {/exp:playa:children}

That should work for you.... if you're inside a channel entries tag, however, you'll need to utilize var_prefix to make it specific to the playa tags.

Make sense?

| improve this answer | |
  • I actually answered my own question prematurly with that same code which worked perfectly when child/ren existed though, it was still giving me the my-div tags when nothing existed. I've since been playing with var_prefix without much success for some reason. I'll keep trying, however. Thanks! – Natetronn May 13 '13 at 20:52
  • Seeing your full code and where you're at would help. Also, if you add an {if no_children} conditional pair, does that change anything? – Lisa May 13 '13 at 20:54
  • no_children has no effect. I'm going to email you at P&T support if you don't mind. – Natetronn May 13 '13 at 21:20
  • No. more. emails. runs and hides I kid you sir. Of course you can email. :) But let us make sure we follow up here with the answer after, yes? :) – Lisa May 13 '13 at 22:30
1

I (we) ended up figuring out a solution!

This after some 30+ back and forth emails with Lisa, both of us learning a bunch of stuff about how Playa - within Matrix - within {epx:playa:children} works. Plus count, row_count and total_results, total_rows and a bunch of other variables for Playa and Matrix plus all the var_prefix scenarios.

Ultimately I finally gave up for the most part at which point Lisa suggested I look into Switchee which I did only to realize I was running into the same count issue I was having before. More on that at the bottom of this answer.

Anyway, this for some reason, out of the blue, gave me an idea to try Column-bo which is similar to Splitter if you are familiar with that add-on.

Column-bo's example shows it being used and wrapped around the channel entry tag so I wasn't quite sure it would work inside the channel entry tag itself and wrapping the {exp:playa:children} tag or not.

I honestly can't believe it but, it did in fact work!

The pseudo code looks like this:

{exp:columnbo:lists columns="1" open='<div class="my-div">' close="</div>" delimiter="</div>" no_results=""}

    {exp:playa:children channel="somechannel" var_prefix="child"}

        {child:matrix_field var_prefix="mx"}

            {if mx:field_row_index == 0}
                <div>
                    <ul>
            {/if}

            {mx:playa_field var_prefix="ply"}                                   

                <li>{ply:title}</li>

            {/mx:playa_field}

            {if mx:row_count == mx:total_rows}
                        </ul>
                    </div>
            {/if}

        {/child:matrix_field}

    {/exp:playa:children}

{/exp:columnbo:lists}

which gives me something like following which is exactly like my original code from my question except that the my-div div now only occurs when we have results where as before it was there with results or without:

<div class="my-div"> <--- 1 my-div no matter how many inner divs, ul, li sets existed
    <div>
        <ul>
            <li>Title 1 A</li>
            <li>Title 1 B</li>
            <li>Title 1 C</li>
        </ul>
    </div>
    <div>
        <ul>
            <li>Title 2 A</li>
            <li>Title 2 B</li>
        </ul>
    </div>
</div>

With Playa, Matrix and their count, row_count and total_rows, total_results variables and conditionals etc. I was only ever able to achieve the following where each playa matrix set of rows is wrapped:

<div class="my-div"> <--- 1 of 2
    <div>
        <ul>
            <li>Title 1 A</li>
            <li>Title 1 B</li>
            <li>Title 1 C</li>
        </ul>
    </div>
</div>
<div class="my-div"> <--- 2 of 2
    <div>
        <ul>
            <li>Title 2 A</li>
            <li>Title 2 B</li>
        </ul>
    </div>
</div>

Ultimately I'm not sure the var_prefixing mattered or not though, I left it for good mesure.

My initial question doesn't do this issue justice, I realize that though, I hope my explanation/answer does help someone.

Thanks to Lisa and P&T Email Support for bearing with me through this one!

Why Column-bo works and Playa/Matrix vars/conditionals does not?

Column-bo is presumably counting the closing divs and then wraps the first and last one with my-div. So, if you look at my html output example above Column-bo is seeing two closing divs based on the delimiter param I gave it to look for (it could see more divs or none even dependent on the current parent entry and the relationships) then it wraps all of those divs with my-div div of course; once since the columns param is set to 1. Also, if there are no_results or no closing divs ie. the dilimiter then it doesn't add my-div which is perfect.

Playa/Matrix on the other hand is seeing the following counts: 1,2,3 & 1,2 (which is Title 1 A-C and Title 2 A-B) from the playa_field's data and why the conditionals using 1 doesn't work as there are two 1's and total_results doesn't work either because there are two total_results if you want to think about it that way. Actually, it does work, as it's doing what it's suppose to do, by wrapping my-div twice but, it's just not doing what I wanted/needed it to do of course.

P.s. I questioned how all this Playa within Matrix with Playa:Children along with everything else I had in my template plus now adding in Column-bo would effect performance. My code is adding 40 queries to the mix. I'm get the following Total Execution time: 0.3644 which is about .1 more than not having the code in place. No caching mind you so, I think it will be fine :)

UPDATE

Ultimately something about using {exp:playa:children} was causing an issue with orderby and sort. I was never able to get things to order correctly so, I scrapped it and went with all the regular tags.

Now everything sorts based on the very first Matrix row order in the entry itself so, if the client moves those rows around everything reorders as expected.

There is some real Inception going on here but it works. I ended up with something like this:

{exp:columnbo:lists columns="1" open='<div class="my-div">' close="</div>" delimiter="</div>" no_results=""}

    {matrix_field var_prefix="child"}

        {child:playa_field var_prefix="play"}

            {play:matrix_field var_prefix="mx"}

                {if mx:field_row_index == 0}
                    <div>
                        <ul>
                {/if}

                {mx:playa_field var_prefix="ply"}                                   

                    <li>{ply:title}</li>

                {/mx:playa_field}

                {if mx:row_count == mx:total_rows}
                            </ul>
                        </div>
                {/if}

            {/play:matrix_field}

        {/child:playa_field}

    {/matrix_field}

{/exp:columnbo:lists}
| improve this answer | |
0

You could wrap your div.my-div tag with an if statement, like so:

{if "{my_playa_field:total_children channel="somechannel"}" > 0}
    <div class="my-div">
{/if}

You could do the same with the closing tag.

I haven't tested this, but I hope it helps. Some more info on the P&T site.

| improve this answer | |
  • For the most part that either threw T_String errors, Invalid EE Conditional Variable PHP errors, did nothing or outputted two my-divs dependent on how I setup the conditionals. The closet I came was this inside the matrix_field: {if playa_field:total_children >= 0} which gave me 2 wrapping my-divs which is the same outcome I was able to get with native Playa and Matrix vars/conditionals as before. Using or not using var_prefix added more complexity to the issue of course though, I tried both routes. Using params threw errors. I tried with brackets, with quotes & brackets and without either. – Natetronn May 16 '13 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.