Forum for discussing general topics related to Couch.
8 posts Page 1 of 1
I have a nested page template called Categories, in which I keep categories and their subcategories. In a particular design I'm working on, I need to display both the category a page is in, plus the top-most root category. Imagine this structure:

- Cat 1
-- Cat 3
--- Cat 4
- Cat 2

If a blog post I have (which is connected to the category through a relation) is in Cat 3 or Cat 4, I'd like to display both that category and Cat 1. (Example:
Code: Select all
<p>Cat 1 / Cat 4</p>
).

I am currently using the following code:
Code: Select all
<cms:related_pages 'blog_category'>
   <cms:pages masterpage='category.php' id="<cms:show k_nested_parent_id />">
      <a class="category root" href="<cms:show k_page_link />" title="<cms:show k_page_title />"><cms:show k_page_title /></a>
   </cms:pages>
   <a class="category" href="<cms:show k_page_link />" title="<cms:show k_page_title />"><cms:show k_page_title /></a>
</cms:related_pages>

At this moment the code I have supports the display of the parent category. However, this only works when I have two category levels. I want to make sure I always get the root displayed (which is basically: the top-most parent which does not have
Code: Select all
k_nested_parent_id
set), no matter how many category levels I have.

Since I could not find a while-loop construct in CouchCMS, I'm stuck on trying to achieve this. Does anyone have an idea? Thanks in advance!
Hello and welcome :)

It seems you are using 'nested-pages' through 'relations' to achieve what the 'folders' do natively.
The only reason why you'd be doing so would be if you want to relate one page with multiple categories/folders. Is that so? If not, could you please let me know why your are not using the simple folders?

The 'relations' feature was actually never meant to be used with the nested-pages (the listing you'd be getting in the admin-panel will be a flat one). So, this scenario is not a standard one for me.

Please give me some time to study it. Let me see if I can come up with something.
Thanks.
Hello and thank you :)

The relation between the blog posts and categories works perfectly. Each blog post can have exactly one category and the code I posted in my question is also doing exactly what I'm expecting it to do.

The point is, I just want to be able to find the root of a category and use its variables. Normally I'd do something like this (semi-pseudo/PHP-code):

Code: Select all
while(true)
{
  if (empty($k_nested_parent_id))
  {
    $root_category = $this_category;
    break;
  }
}


And then just do whatever I want with $this_category.

The reason why I'm not using folders is because these have to be hard-coded somewhere and I do not want that. I want the user of the CMS to be create and nest whatever category she wants, without having to consult me, or edit a PHP file. Besides that, the relation, like I mentioned, does everything I want it to do, so that's great. In this case, I don't mind that the relation listing is flat.
If you use dynamic_folders='1', no editing of PHP files is required.
I agree with cheesypoof.
Your client can create folders on her own now - please see http://www.couchcms.com/docs/miscellane ... lders.html

While using relations could do the same thing, use of folders will have the following advantages -
1. SEO friendly - the URL of cloned pages will have all the parent folders added to it (if using prettyURLs)
2. Easier to work with as the 'folder-view' is natively supported.
3. For your case in point, we can use cms:parentfolders tag to get the parent hierarchy (http://www.couchcms.com/docs/tags-refer ... lders.html)

Would it be too difficult for you to convert to using folders instead of relations?
Well, this part of the documentation went past me, clearly. I see I can use cms:parentfolders to find the top-most folder. (I assume that in this case, my first result would be Cat 1). Thanks for the hint!

I was using folders for something else on my blog, but those folders aren't necessary per se, so I may just switch over to dynamic folders.

I just noticed that cms:parentfolders has no limit. I think either the ability to limit a loop or break from a loop, since often I don't want a complete result, but don't want to loop through all results unnecessary. (I also really miss this with repeatable editables, unless I've missed that part of the documentation as well). So, I'm still curious if that's possible in either way?
I think either the ability to limit a loop or break from a loop, since often I don't want a complete result, but don't want to loop through all results unnecessary. (I also really miss this with repeatable editables, unless I've missed that part of the documentation as well). So, I'm still curious if that's possible in either way?

Unlike PHP, Couch does not have a conditional 'break' to curtail a loop.
However, we can still use some conditional logic with a counter to output only the desired number of rows. For example, the following snippet will output only the topmost parent although the cms:parentfolders tag will iterate through all ancestors -
Code: Select all
<cms:parentfolders folder='china' masterpage='sometemplate.php'>
    <cms:incr my_counter />
    <cms:if my_counter='1'>
        <a href="<cms:show k_folder_link/>"><cms:show k_folder_title/></a>&nbsp;>
    </cms:if>
</cms:parentfolders>

The key, of course, is the use of cms:incr to increment a variable named 'my_counter' at every iteration.
The cms:show_repeatable tag already has a counter named 'k_count' built-in so that can be used while dealing with repeatable regions.

Hackish, I know. But the loops we'd be dealing with will not be lengthy so should get the job done.
Thank you for your example.

This solution had come to my mind too. This does mean that when I have a long list of items, the loop is going to be initiated many times (so many if-statements to execute). Likely with the list my client's going to make, the performance hit will be minimal, so either I'll cache the blog posts or leave it all be.

I'll be making some dynamic folders now. I think it's a great feature and I suggest moving it from the Miscellaneous section of the documentation to a more visible site (maybe even Core Concepts, since this seems rather essential for blogging to me).
8 posts Page 1 of 1