Accessing category children...

Moved Technical Support
  • #1

    The latest in my how do I.. on the new theme.

    I'm making use of the subcategories thus only displaying the parents on the home.

    I wanted to add a toggle to display a div containing the categories.children members and managed to do so but didn't find a clean way to do it.

    <!-- IF categories.children --> and <!-- IF children --> didn't seem to do the trick. Is there an exists? or the like construct?

    So I ended up doing

    <!-- BEGIN children -->
    <!-- IF @first -->

    This works but it's obviously inelegant at best. 🙂

    The other thing that seems to fail as a result, and I'm not certain why but it may be just another bag of bad assumptions, is the actual child div contents.

    As a shortcut I pulled the included partial from the categories page for the category children and used it. It.. kind of works.. But, well, look for yourself. The same partial works fine on the categories page.


    You can see it's iterating through (the correct number ) of children, but for some reason it's not substituting the values it should.

  • #2

    You need to do {}

  • #3


    I'd tried that, I though, so I went and did it again.

    If I leave the BEGIN as is, and change the partial to use categories.children.* it behaves the same way.

    If I do the BEGIN with categories.children, it's blank - but I expected that.

                        <!-- BEGIN children -->
                        <!-- IMPORT partials/home_child.tpl -->
                        <!-- END children -->

    home_child.tpl is a copy of the categories_child.tpl with every children.* changed to a categories.children.*

    Is it possible that we cannot access an inner array's contents with templatejs?

  • #4


    <!-- IF categories.children.length --> does not seem to work. I'd like to ditch that kludge. Am I calling it wrong?

  • #5

    Sorry I'm on my phone it won't be actual code but I actually did that. The fact is that in home categories object list categories and children is one of the keys with an array of children. Begin is used to go through an array. So on home it's :
    Begin categories
    Begin categories.children

    While on a category page there's just one category object with a children array so you just need to Begin children and do category.children.*

  • #6

    Hmm. That's totally not working in my case.

    I wonder if there's a regression.

    Again, this is on home.tpl.

    When I change the inner loop to categories.children, no iterations. ( I was assuming there was an implicit "With" type behavior going on here.)

    When I do it on children, it works, but I can't bust out the values.

  • #7

    @Shard Ok I'm on PC now. I'll share a bunch of code to explain you how I achieved that in my theme.
    I have two partials, one called category_children.tpl and another categories_children.tpl.
    Both share the exact same code the only difference is one is used on homepage and the other on category pages.
    In my theme I show only subcategories. Using parents categories as groups of categories.
    In order to test if it's sub-caregories I do

    <!-- BEGIN categories -->//Start going through array
    		<!-- IMPORT partials/categories_children.tpl -->//Call to my subcategory template
    <!-- END categories -->

    And then in my subcategory

    <!-- BEGIN children -->
    <!-- END children -->

    The difference in category_children.tpl is that {categories.children.whatever} is replaced by {children.whatever} as simple as that.
    Take a look at and to understand.

  • #8

    @esaio The only difference in your snippets and mine are that my import was also inside of the children loop.


    This is a good laugh. It seems that whenever I did my %s/ on children. to categories.children I didn't bother to spell children right or ever properly read my output.

    So yeah.

    Working now.

  • Plugin & Theme Dev Anime Lovers GNU/Linux

    Thanks @esiao I've been wondering how this would look.

Suggested Topics

| | | |