Publisher does not support the Fluid field type. Please do not contact asking when support will be available.

If you purchased an add-on from expressionengine.com, be sure to visit boldminded.com/claim to add the license to your account here on boldminded.com.

Ticket: Nested Bloqs variables not parsed in closing tag after first bloq.

Status Resolved
Add-on / Version Bloqs 4.0.9
Severity
EE Version 4.3.6

Jarod Clark

Nov 07, 2018

Description: I have a setup where I’m using nested Bloqs. I’m trying to do a conditional in the close section for a bloq and the variable is only parsed for the first instance of that bloq type.

Here’s an example of my code:

{sections}

	{!-- ROOT: SECTION --}
    {section}
	    <div class="{if theme == 'light'}bg-gradient-light{/if} px-xs relative clearfix" data-block-name="section">
	    	{if container == 1}<div class="container mx-auto">{/if}

	    {close:section}
	    	{container}
	    	{if container == 1}</div>{/if}
	    </div>
	    {/close:section}
    {/section}
    {!-- END ROOT: SECTION --}

{/sections}

As you can see, I have a {container} variable that I’m trying to run a conditional on in the closing tag. The first time this bloq is displayed, everything works fine (and it outputs 1 in this case). The second time I have a bloq of that same type on the page, it’s not parsed and just outputs {container}.

The variable and conditional are properly parsed in the main part of the tag, it’s just whatever is in the {close:section} part that isn’t parsed.

#1

BoldMinded (Brian)

What is “container”? Is it a block variable or something else? Another custom field?

#2

BoldMinded (Brian)

Is container an atom?

#3

Jarod Clark

Sorry for the delay.

“container” is an atom (a field defined in the bloq type). In this case, it’s a toggle field.

#4

BoldMinded (Brian)

Thanks, I’ll try to replicate it soon and see what the issue is.

#5

BoldMinded (Brian)

I was not able to replicate this. Here is my section code, which I added a Toggle field too, then added a section > row, section > row to the Bloqs field.

Have you tried a similar conditional with another atom type? E.g. checking if a field value == some text instead of using a Toggle field? What other ways have you tried to debug this?

{section}
            <div class="container" data-id="{blocks:id}" data-type="section">
            <h1>{heading}</h1>
            container: {container}
            {close:section}
             <div class="closing">{if container}Yep{/if}</div>
            </div>
            {/close:section}
        {/section}

https://screenshots.firefox.com/uWEhr5MZXK9P1IQB/ee400.vm

#6

BoldMinded (Brian)

https://screenshots.firefox.com/hqZvymOc7wXCvQKh/ee400.vm

#7

Jarod Clark

I just set up another test page with only 3 top level bloqs, no nesting. It seems it’s the last bloq that isn’t being parsed.

http://www.unleadedsoftware.com/tmp/bloqs-entry.png http://www.unleadedsoftware.com/tmp/bloqs-frontend.png

#8

Jarod Clark

And actually, in looking at this a bit closer, in my debug code where I’m outputting the bloqs variables in the close tag, it’s actually outputting the variables from the NEXT bloq, not the current one.

So if you look at that bloqs-frontend image I linked in the previous comment, it should be outputting 1 - white, then 0 - light, then 1 - dark based on the values I have in the bloqs. However, it’s only outputting from the 2nd and 3rd bloqs, and then outputting no parsed values for the last iteration.

Hopefully that helps with tracking this down.

#9

Jarod Clark

Comment has been marked private.

#10

BoldMinded (Brian)

Ok, I can see conditionals not parsing correctly, but it appears to only be the Toggle fields, as in its not correctly evaluating “0” as false and “1” as true. The field values are strings, not integers.

Given the following code where container is a Toggle and theme is a dropdown, the theme field conditional appears to be working, just not the Toggle field.

{section}
            <div class="container" data-id="{blocks:id}" data-type="section">
            <h1>{heading}</h1>
            theme: {theme} {if theme == "light"}bright{/if}{if theme == "medium"}dull{/if}{if theme == "dark"}boring{/if}
            container: {container} {if container}Yep{/if}
            {close:section}
             close container: {container}
             <div class="closing">{if container}Yep{/if}</div>
            </div>
            {/close:section}
        {/section}

https://screenshots.firefox.com/c6uCI0iDwKU86WPC/ee400.vm

#11

BoldMinded (Brian)

https://screenshots.firefox.com/0zIe4uxqRjZwezLT/ee400.vm

#12

BoldMinded (Brian)

Comment has been marked private.

#13

BoldMinded (Brian)

I think this has something to do with the internal parser. If I explicitly set the toggle field value to false if it has the string value of “0” then the template renders correctly.

Try making this code change:

protected function _parseConditionals($tagdata, TagOutputBlockContext $context)
    {
        // Compile conditional vars
        $cond = [];
        $cond = array_merge($cond, $this->getContextVariables($context));
        $block = $context->getCurrentBlock();

        // Map column names to their values in the DB
        foreach ($block->atoms as $atom) {
            // Add this conditional...
            if ($atom->value === '0') {
                $atom->value = false;
            }
            $cond[$atom->definition->shortname] = $atom->value;
        }

        $tagdata = $this->EE->functions->prep_conditionals($tagdata, $cond);

        return $tagdata;
    }[/code]

My latest template code with results: 

[code]
{section}
            <div class="container" data-id="{blocks:id}" data-type="section">
            <h1>{heading}</h1>
            theme: {theme} {if theme == "light"}bright{/if}{if theme == "medium"}dull{/if}{if theme == "dark"}boring{/if}
            container: {container} {if container}Yep{if:else}Nope{/if}
            {close:section}
             close container: {container}
             <div class="closing">{if container}Yep{if:else}Nope{/if}</div>
            </div>
            {/close:section}
        {/section}

https://screenshots.firefox.com/5Hyw8H2aIgmrm5ei/ee400.vm

#14

BoldMinded (Brian)

Note, this is when the field is not set to nestable. When I make it nestable I do see the issue of the conditional inside the close tag showing the incorrect value.

#15

BoldMinded (Brian)

Comment has been marked private.

#16

Jarod Clark

I installed that build you provided and this does appear to be working properly now.

Here’s output from a test template I set up this morning which was showing incorrect info before I updated:

Start Section
 Container: 1
 Theme: white
    

    Start Close Section
     Container: 1
     Theme: white
    End Close Section
End Section
    
Start Section
 Container: 0
 Theme: light
    

    Start Close Section
     Container: 0
     Theme: light
    End Close Section
End Section
    
Start Section
 Container: 1
 Theme: dark
    

    Start Close Section
     Container: 1
     Theme: dark
    End Close Section
End Section
#17

BoldMinded (Brian)

Comment has been marked private.

#18

Jarod Clark

Just uploaded that latest build, and it all looks to be working fine still.

#19

BoldMinded (Brian)

Sweeeet. Thanks for checking. I’ll go ahead and close this ticket.

Thanks for pointing out this bug btw, seems its been like this for months and no one has caught it :(

#20

BoldMinded (Brian)

Jarod, I took another look at the conditional parsing, specifically the {if container} conditional, and found that it was working as intended. You’ll need to change them to {if container == ‘0’} or {if container == ‘1’} in order for it to work. I found this in EE’s core, which I should follow, otherwise some other field that actually contains the value “0” that is not a boolean will not evaluate correctly in the conditional.

/**
  * Cast to EE boolean
  */
 private function bool($value)
 {
  // We do *not* follow the string 0 php casting rule.
  // {if field} should be true if the user entered
  // something. Doesn't matter what it is.
  if ($value === '0')
  {
   return TRUE;
  }

  return (bool) $value;
 }

Login to reply