Problems, need help? Have a tip or advice? Post it here.
4 posts Page 1 of 1
I'm displaying a sidebar (snippet) everywhere on the blog views (folders & pages).
I'm using dynamic folders.
If I'm on a page or in a folder view I would like to check if the current folder k_page_foldername or k_folder_name has child-folders or not. If it is not I would like to display the "sibling folders" instead of the child folders. Is this possible? How do I check for this?
If already tried to setup the logical structure.

Code: Select all
<cms:if k_is_page>
      <cms:if k_page_folder has children><!-- how to check for this? -->
             <cms:folders masterpage='blog.php' childof=k_page_foldername depth='1' >
                  <li>
                      <a href="<cms:show k_folder_link />">
                           <cms:show k_folder_title />
                       </a>
                   </li>
               </cms:folders>
        <cms:else />
            <!-- display sibling folders here (how to do this?) -->
         </cms:if>
<cms:else />
    <cms:if k_folder has children><!-- how to check for this? -->
          <cms:folders masterpage='blog.php' childof=k_folder_name depth='1' >
                  <li>
                      <a href="<cms:show k_folder_link />">
                           <cms:show k_folder_title />
                       </a>
                   </li>
               </cms:folders>
        <cms:else />
            <!-- display sibling folders here (how to do this?) -->
         </cms:if>
</cms:if />
Hi,

Following is one way of doing what you want.

The code for listing children/siblings is almost identical for both the 'page_view' and the 'folder_view' so I have placed that part of the code in a snippet (this way we can reuse it at the two places). I've also profusely commented it to make it easier for you to follow what is going on.

Please create a snippet named 'list_folders.html', place it within the 'couch/snippets' folder (or if you have specified a custom location in config, place the file there). Paste the following code within it -
Code: Select all
Folder is: <cms:show my_current_folder /><br />

<cms:folders root=my_current_folder depth='1'> <!-- fetch the folder in question -->
    <cms:if k_folder_immediate_children > <!-- if this folder has any subfolders -->
        Folder has subfolders: <br />
        <ul>
        <cms:folders childof=my_current_folder depth='1' > <!-- get those subfolders -->
            <li>
                <a href="<cms:show k_folder_link />"><cms:show k_folder_title /></a>
            </li>
        </cms:folders>
        </ul>
    <cms:else /> <!-- if the folder does not have any subfolders, show siblings -->
        Folder does not have subfolders. Show siblings (i.e. chilren of parent): <br />
        <!--
            to show the siblings, however, we need to know the parent folder's name.
            Unfortunately at this point Couch only provides the 'id' of the parent folder
            so we need to convert the id to name.
            To do that we use cms:parentfolders to loop through this folder's parents
            looking for the matching 'id'.
        -->
        <cms:set my_folder_parentid = k_folder_parentid />
        <cms:set my_folder_parentname = '' />
       
        <cms:parentfolders folder=my_current_folder >
            <cms:if k_folder_id = my_folder_parentid >
                <cms:set my_folder_parentname = k_folder_name scope='parent' />
            </cms:if>   
        </cms:parentfolders>
       
        <!--  we have the parent folder's name here (may be blank) -->
        Parent: <cms:show my_folder_parentname /> <br/>
        Children of parent (excluding current folder) are: <br />
        <ul>
        <cms:folders childof=my_folder_parentname exclude=my_current_folder depth='1' > <!-- get its children -->
            <li>
                <a href="<cms:show k_folder_link />"><cms:show k_folder_title /></a>
            </li>
        </cms:folders>
        </ul>
    </cms:if>
</cms:folders>

Now in you main template where you wish to do the listing, place the following code that uses the snippet -
Code: Select all
<cms:if k_is_page >
    <h1>Page_view <cms:show k_page_title /></h1>
   
    <cms:if k_page_foldername > <!-- if current page is within a folder -->
        <cms:set my_current_folder = k_page_foldername />
        <cms:embed 'list_folders.html' />
    </cms:if>

<cms:else_if k_is_folder />
    <h1>Folder_view <cms:show k_folder_title /></h1>
   
    <cms:set my_current_folder = k_folder_name />
    <cms:embed 'list_folders.html' />

</cms:if>

You can tailor the markup as you desire.

Hope it helps.
Thank you KK for the fast answer!
It didn't work a 100% like I wanted, but I tweaked it a little bit and now it is completely how I had it in mind :D
Thanks you so much for getting me started, without this information I wouldn't have gotten there... In case you are wondering what I changed here is my final code in my snippet I embeded in my blog templates:


Code: Select all
<h4>Categories</h4>
<ul class="nav nav-list primary">
    <cms:if k_is_page >
        <cms:if k_page_foldername > <!-- if current page is within a folder -->
            <cms:set my_current_folder = k_page_foldername />
        </cms:if>
    <cms:else_if k_is_folder />
        <cms:set my_current_folder = k_folder_name />               
    </cms:if>
    <cms:folders root=my_current_folder depth='1'>
        <cms:if k_folder_immediate_children >
            <cms:set folder_level='children' scope='global' />
        <cms:else />
            <cms:set folder_level='siblings' scope='global' />
            <cms:set my_folder_parentid = k_folder_parentid />
            <cms:set my_folder_parentname = '' />
            <cms:parentfolders folder=my_current_folder >
                <cms:if k_folder_id = my_folder_parentid >
                    <cms:set my_folder_parentname = k_folder_name scope='global'/>
                </cms:if>   
            </cms:parentfolders>
        </cms:if>
    </cms:folders>

    <cms:if folder_level='children' >
        <cms:folders childof=my_current_folder depth='1' >
            <li><a href="<cms:show k_folder_link />"><cms:show k_folder_title /></a></li>
        </cms:folders>
    <cms:else />
        <cms:folders childof=my_folder_parentname depth='1' > <!-- get its children -->
            <li><a href="<cms:show k_folder_link />"><cms:show k_folder_title /></a></li>
        </cms:folders>
    </cms:if>                         
</ul>
I'm glad my reply helped, @cowgirl :)
4 posts Page 1 of 1