Problems, need help? Have a tip or advice? Post it here.
3 posts Page 1 of 1
Hello Guys,

I'm fairly new on CouchCMS and i'm trying to get my first project started with it.

Right now, i'm struggling with the loops.
The site should be a collective of artist, each one with their own projects and products, so the structure should be:

Home
--Artist A
----Projects
------Project 1A
------Project 2A
----Products
------Product 1A
------Product 2A
--Artist B
----Projects
------Project 1B
------Project 2B
----Products
------Product 1B
------Product 2B

I'm using nested folders and despite the "name" is different (like projects-1 and so on) the title "Projects" is always the same (also the "Products")

I made one single template that when browsing "Projects" or "Products" it correctly shows the childs of this folders, and when browsing Home it shows the links to each artists.

But i would also like to show in the Home "sections" of each artist like this:

Bla Bla Bla Artist Name
Current Projects:
Some content from project 1
Some content from project 2
Some content from project 3
More bla bla bla
Current Products:
Some content from product 1
Some content from project 2

Can i do some kind of loop that only goes through childof "parent_title" instead of "parent_name"?
That way i would not need to edit the template everytime i add a new artist to the site.

Rigth now i'm using:

Code: Select all
<cms:nested_pages include_custom_fields='1' depth='2' order='asc' childof=k_page_name >
      <a href="<cms:show k_nestedpage_link />">
        <h1><cms:show k_nestedpage_title /></h1>
      </a>
<cms:if k_nestedpage_parent_title='Project' >
Test test project
</cms:if>
</cms:nested_pages>


But i'm not sure if this is code efficient as it will loop through all the folder hierarchy every time.

Best regards

PS: Sorry for my english
Hi and welcome :)

Nested pages are primarily meant to create menus and are not really suited for holding contents.

Couch's recommended way of handling your use-case would be through the use of three separate clonable templates ('artists', 'projects', 'products') and then relating the three together.

However, since you have already implemented your solution using a single nestable template, let us try and solve the problem continuing with it.

The tree that you have has four levels - 1. home, 2. artists, 3. projects/products, 4. actual pages.

Depending on which level page a user is visiting, you'll want to display the contents in a different manner (e.g. when on an 'artist' page show the projects/products below only that artist etc.).

So to begin with we'll have to determine which level page we are on. Unfortunately Couch does not provide any ready-to-use variable giving us this value so we'll have use a little workaround.

We'll use the cms:nested_crumbs tag. It iterates and shows us the hierarchy of whatever page we are on e.g. if we are on Project 1A it'll show -
Home » Artist A » Projects » Project 1A

Instead of using it to show a tree of hierarchy, we'll simply use it to calculate the number of levels shown e.g. in the example above 4 levels are being shown so our page is at 4th level.

Place the following in your template and then we'll have a variable named 'my_level' that will give us this info.
Code: Select all
<cms:set my_level='0' 'global' />
<cms:nested_crumbs >
    <cms:set my_level="<cms:add my_level '1' />" 'global' />
</cms:nested_crumbs>


We know any of the 'artist' pages will be at level '2'. We can now use this info to loop through its top level children. We know that there are only two children - the first is 'Project' and the other is 'Products' so we can make a distinction between the two children -
Code: Select all
<cms:if my_level='2'>
    <h2>Artist Name: <cms:show k_page_title /></h2>
   
    <cms:nested_pages depth='1' order='asc' childof=k_page_name >
        <cms:if k_first_child >
            <h3>Current Projects:</h3>
            ..
        <cms:else />
            <h3>Current Products:</h3>
            ..
        </cms:if>
    </cms:nested_pages>
</cms:if>

Now within each of those two children we have above, we can use cms:nested_pages tag once again to show their respective children i.e. the actual projects and product pages.
The amended code now becomes -
Code: Select all
<cms:if my_level='2'>
    <h2>Artist Name: <cms:show k_page_title /></h2>
   
    <cms:nested_pages depth='1' order='asc' childof=k_page_name >
        <cms:if k_first_child >
            <h3>Current Projects:</h3>
            <cms:nested_pages include_custom_fields='1' depth='1' order='asc' childof=k_nestedpage_name >
                <h4><cms:show k_page_title /></h4>
                ..
            </cms:nested_pages>
        <cms:else />
            <h3>Current Products:</h3>
            <cms:nested_pages include_custom_fields='1' depth='1' order='asc' childof=k_nestedpage_name >
                <h4><cms:show k_page_title /></h4>
                ..
            </cms:nested_pages>
        </cms:if>
    </cms:nested_pages>
</cms:if>

As I said, it is a bit convoluted but can be done.
Hope it helps.
Thanks a lot KK,

With your examples and a bit of tweaking i was able to loop through.

The key was using "cms:capture" to set a global variable (i found this here: viewtopic.php?f=4&t=9302)
Code: Select all
<cms:set has_projects='0' 'global' />
<cms:capture into='this_projects' >
<cms:nested_pages include_custom_fields='1' depth='1' order='asc' childof=k_page_name >
   <cms:if k_nestedpage_title='Projects'>
      <cms:show k_nestedpage_name />
   </cms:if>
</cms:nested_pages>
</cms:capture>

This will just fetch the current artist "Projects" folder name into a variable (this_projects).

So you can now use it here:
Code: Select all
<cms:capture into='anything_notused' >
   <cms:nested_pages include_custom_fields='1' depth='1' order='asc' childof="<cms:show this_projects />">
      <cms:if "<cms:not_empty k_nestedpage_title />">
         <cms:set has_projects='1' 'global' />
      </cms:if>
   </cms:nested_pages>
</cms:capture>

And loop through the projects of this artist to see if there is one at least.

Then, a "<cms:if has_projects>" let me show the "Projects" section of this artist only if they have any project at all and i can keep looping to the next artist (or section).

Best regards and i hope this help someone else too
3 posts Page 1 of 1