Recursion from folder to root searching for a specific block
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
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
1 Posted by Nando on 22 May, 2016 02:02 AM
I'm actually looking for a navigation block like that, this is my code:
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 Posted by Ryan Griffith on 23 May, 2016 12:45 PM
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.
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 Posted by jbenoit on 23 May, 2016 04:14 PM
Nando's code did the trick! Awesome work!
4 Posted by Nando on 23 May, 2016 04:22 PM
awesome! please share any changes/enhancements :)
5 Posted by Ryan Griffith on 23 May, 2016 05:01 PM
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!
Ryan Griffith closed this discussion on 23 May, 2016 05:01 PM.