Recursion from folder to root searching for a specific block

jbenoit's Avatar

jbenoit

19 May, 2016 06:43 PM

I am trying to develop a way to allow sub pages to have different configurations based on a "_config" block in their folder, or a parent folder, or a parent folder above that, so on until the root of the site is reached. The root of the site is guaranteed to have a "_config" block, but in searching recursively up the folder hierarchy, I am running into a max recursion issue, and I can't figure out why.

The code I have been trying to refine is below:


#set ($output = $currentPage.parentFolder)
$output.path

#test($output)

#macro (test, $output )
    Locate Block Output: $output.path
    #if ($output.path != '/')
        #set ($directory = "${output.path}/")
    #else
        #set ($directory = $output.path)
    #end
    #if ($_PropertyTool.isNull($_.locateBlock("${directory}_config", $currentPage.siteName)))
        ${output.path}_config
        No Config Block  
    #else
        Found a Config Block
    #end
    #if ($output.path != '/')
        #set($output = $output.parentFolder)
        #test($output)
    #end

#end
  1. 1 Posted by Nando on 22 May, 2016 02:02 AM

    Nando's Avatar

    I'm actually looking for a navigation block like that, this is my code:

    #set ($pagelink = $_XPathTool.selectSingleNode($contentRoot, "//system-page[@current]/path").value)##
    #set ($navlocation = "")
    #set ($navpath = $pagelink)
    #set ($nlocation = $navpath + ("/navigation"))
    ##$nlocation
    #set ($navcheck = $_.locateBlock($nlocation, $currentPageSiteName))
    #if($navcheck)
    #set ($nlocation = $navlocation)
    #else
    #set ($folders = $navpath.split("/"))
    #set ($paths = [])
    #foreach ($folder in $folders) 
    #set ($add = $paths.add($folder))
    #end
    #set($c = $paths.size())
    #foreach ($path in $paths)
    ##$c
    #set($c = $c - 1)
    #set($path = $paths.get($c))
    ##$path
    #set ($navpath2 = $_StringTool.substringBefore($pagelink,$path))
    #set ($nlocation2 = $navpath2 + ("navigation"))
    #set ($navcheck2 = $_.locateBlock($nlocation2, $currentPageSiteName))
    ##$nlocation2
    #if($navcheck2)
    #set ($navlocation = $nlocation2)
    ##$navlocation YAY
    #break
    #end
    #end
    #end
    #set ($navigation = $_.locateBlock(${navlocation}, $currentPageSiteName))
    

    I'm starting at the current page path and checking for the nav block. if its not there, I keep going up directories till I find it.

    not the prettiest code but it gets the job done, if you (or anyone) has any recommendations - post'em up :)

  2. 2 Posted by Ryan Griffith on 23 May, 2016 12:45 PM

    Ryan Griffith's Avatar

    Hi,

    Unfortunately, this is a limitation imposed by the Velocity engine so there really isn't much that can be done. I have read there is a max depth property that could be set for macros, but there is no official documentation for it on Apache's website.

    It might be possible to increase this limit using the property; however, we would not support the change since it could potentially have a negative impact on performance.

    I am trying to develop a way to allow sub pages to have different configurations based on a "_config" block in their folder, or a parent folder, or a parent folder above that, so on until the root of the site is reached

    One suggestion I can make is to apply a specific Metadata Set to these setup blocks and use the Query API to search for all Blocks with that specific Metadata Set and sort by path. From there, you could probably do something like continue to compare the path of the page with the returned blocks until you find a suitable Block. OR, switch to using an Index Block that includes only Block content so you can easily grab the first Block found within the calling page's hierarchy.

    Please let me know if you have any questions.

    Thanks!

  3. 3 Posted by jbenoit on 23 May, 2016 04:14 PM

    jbenoit's Avatar

    Nando's code did the trick! Awesome work!

  4. 4 Posted by Nando on 23 May, 2016 04:22 PM

    Nando's Avatar

    awesome! please share any changes/enhancements :)

  5. 5 Posted by Ryan Griffith on 23 May, 2016 05:01 PM

    Ryan Griffith's Avatar

    Thank you for following up. I am glad to hear Nando's code did the trick.

    I'm going to go ahead and close this discussion, please feel free to comment or reply to re-open if you have any additional questions.

    Have a great day!

  6. Ryan Griffith closed this discussion on 23 May, 2016 05:01 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM