Determine asset depth in a format?
I have a folder index block that indexes every page, file, and folder in my site, beginning at the base folder. Is there any way to determine how deep a particular asset is in the folder structure as I loop through them?
In plain language, I'd like to do the following: for each asset, print the asset name and how deep it is nested within in my site.
Comments are currently closed for this discussion. You can start a new one.
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 Ryan Griffith on 17 May, 2016 07:52 PM
Hi Rob,
I believe the best way to accomplish this would be to look at its path and count the number of
/
characters.Paths always begin with
/
, so you would need to determine if you want depth of one to mean root level, or if you would want to subtract one and use 0 to indicate root level.Please let me know if you have any questions.
Thanks!
2 Posted by Rob Knight on 18 May, 2016 02:49 PM
Hi Ryan,
Thank you for answering my question. That makes sense. Do you have a code snippet in either XSL or Velocity that does something like that? Even something similar would help, as I'm not sure where to start counting occurrences of a character within a string.
Thank you!
3 Posted by Ryan Griffith on 18 May, 2016 03:55 PM
Hi Rob,
Velocity would be the easier option. Consider the following snippet to help get you started:
To summarize: the
$depth
variable will be value 1 if the asset is within the base folder, because it's path looks like/about
. The variable would be 2 if the asset is one level under the base folder, because it's path looks like/folder/about
. And so on.As I mentioned, you would need to determine how you want to denote "root level," do you want a value of 1, or a value of 0. If you want to go with 0, you would need to tweak the format slightly to subtract one from the depth:
Please let me know if you have any questions.
Thanks!
4 Posted by Rob Knight on 05 Jun, 2016 05:46 AM
This was extremely helpful. Thank you, Ryan.
5 Posted by Ryan Griffith on 06 Jun, 2016 12:02 PM
Not a problem at all, Rob. I am glad to hear my suggestion helped out.
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 06 Jun, 2016 12:02 PM.