How to target all Categories with parent ID == '#'

TradingboTradingbo Member Posts: 24
edited May 3 in Customizations

Hi everyone,

Relatively new to LS & .rain and trying to get my head around something.

How to target all sub-categories within a given parent category using rain/twig in a given template.

Am trying to achieve something akin to this:


{% if category.textpage and parent.category_id == '8661854' } 

<script type="text/javascript">

// do something

</script>

{% endif %}


Can't seem to make the syntax work for some reason. Can anyone point me in the right direction?

The only reference to parent cats i can find in the documentation is this:

categories.{{ category.id }}.parent

Which presumably only returns the parent of a single specific category.

( also slightly confused about how to express the ID in this example as: {{categories.{{ ' 8661854'}} .parent}} returns a fatal error ( as two sets of curly brackets).

Many Thanks,

Answers

  • maurisourcemaurisource Partner Posts: 26 partner

    Hi, in PHP I would do something like that to filter CategoryID and ParentID:

    $categoryID = '8661854';

    $parentID = $json['Category']['ParentID'];


    Now you can just do your matching logic.

    if ($categoryID == $parentID ){echo "we got a parent category"}

  • TradingboTradingbo Member Posts: 24

    Hi Maurisource,

    Thanks for the helping hand. That's pretty useful.

    So using rain & twig this would read as?

    {% if category.id == parent.id %}
    // do something 
    {% endif %}
    


  • TradingboTradingbo Member Posts: 24

    Hi Maurisource,

    Thanks for the link, yes am already pretty familiar with all the above docs.

    I generally post here as a last resort. I've noticed however that unless you are highly familiar with TWIG and the LS usage of it there are some pretty basic syntaxes not covered in the docs.

    E.g. the docs state the syntax for grabbing the category id as:

    categories.{{ category.id }}.id
    

    So you would assume checking the category of a given collection would be something along the lines of:

    collection.categories[6494135].id
    

    However this isn't the case as when it's not inside a loop it requires an _ in the syntax. E.g.

    collection.category_id == '6494135' 
    

    There are a myriad of tiny examples of syntax usage like this which aren't explained well in the Docs and won't be covered in TWIGs docs either.

    Simply makes it difficult for new developers to get familiar and engage with the platform.

    Seems this forum is primarily aimed at either CMS users or heavy-duty API developers, with not much middle ground for companies who already use OnSite or Retail POS and simply need a theme re-designed rather than developed from scratch .

    Thanks for your help though.

Sign In or Register to comment.