2

I'm using channel:categories and would like to show only child categories so I added the following conditional {if {parent_id} != 0}...{/if} but get php errors as soon as I add it to the template. I tried any sort of variable check {category_description} and get errors.

The above conditional was taken from an accepted answer on another thread.

Parse error: syntax error, unexpected '{' in path\to\system\expressionengine\libraries\Functions.php(689) : eval()'d code on line 43

My template looks like this:

{!--stash 1st level category list--}
{exp:stash:set name="categories" parse_tags="yes"}{exp:gwcode_categories channel="products" depth="1" style="linear" backspace="1"}{cat_url_title}|{/exp:gwcode_categories}{/exp:stash:set}

{!--category ids for sub cats--}
{exp:stash:set name="sub_category_ids" parse_tags="yes"}{exp:gwcode_categories channel="products" output_depth="2" style="linear" backspace="1" cat_url_title="{segment_3}"}{cat_id}|{/exp:gwcode_categories}{/exp:stash:set}


{exp:stash:parse}
{exp:switchee variable="{segment_2}" parse="inward"}
  {case value="#^P(/d+)$#|category|''"}


    {switchee variable="{segment_3}" parse="inward"}

      {case value="{stash:categories}"}{!--level 1 category listing filtered by stashed list --}

        {exp:low_variables:single var="lv-products-cat-tier1" 
          preparse:pre_seg_id="{stash:sub_category_ids}"
          preparse:pre_single_cat_id="{segment_3_category_id}"
        }

      {/case}{!--level 1 category listing--}

        some more case checks here

    {/switchee}{!--end nested switchee--}

{/case}{!--listing & category listing--}

some more case checks here
{/exp:switchee} 

Finally my lv-products-cat-tier1 is a low variable with the following code where the parent conditional will wrapp the double-square div when it is working:

        {!--list sub categories--}
        {exp:channel:categories channel="products" show="{pre_single_cat_id}|{pre_seg_id}" style="linear"}

{if {parent_id} != 0}
  yo
{/if}

        <div class="col-xs-6 double-square">
            <img src="http://lorempixel.com/545/270/people" alt="" class="img-responsive">
            <div class="row">
            <div class="pos-ab">
                <div class="col-xs-6 left">
                  <div>
                    <h4><a href="/products/category/{category_url_title}">{category_name}</a></h4>
                  </div>
                </div>

                <div class="col-xs-6 right">
                  <div class="content">
                    {exp:eehive_hacksaw chars="" words="15" cutoff="" append="" allow="<p><b><a>"}
                      {category_description}
                    {/exp:eehive_hacksaw}
                  </div>
                </div><!--right-->
            </div><!--pos-ab-->
            </div><!--row-->
          </div><!--double square-->



        {/exp:channel:categories}

1 Answer 1

7

The problem with that is that if {parent_id} doesn't get replaced with a number before the conditional is evaluated, then EE will choke because when converted to PHP it becomes:

if ({parent_id} == 0)

Either this

{if parent_id != 0}

Or this

{if "{parent_id}" != "0"}

Is more fault tolerant when the variable doesn't exist.

But, you may wonder, why is your conditional getting parsed before {parent_id} has been replaced in the first place? Because by default {exp:stash:parse} parses conditionals after it has parsed the layers of tags it wraps to a "depth" of 3 (parse_conditionals="yes" and parse_depth="3" by default).

It so happens that in your code the {exp:low_variables} tag is on the deepest layer to be parsed (layer 3). This tag gets replaced with your {exp:channel:categories} tag pair which is NOT parsed, because at this point Stash is done parsing and will now parse any conditionals that have been exposed. See the problem?

To stop this undesirable behaviour you can either set parse_conditionals="no" on the {exp:stash:parse} tag or increase the parse depth with parse_depth="4" so that the categories tag is fully parsed before the conditionals are evaluated.

1
  • That's a proper answer.
    – foamcow
    Jun 12, 2014 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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