Determine asset depth in a format?

Rob Knight's Avatar

Rob Knight

17 May, 2016 07:36 PM

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.

  1. 1 Posted by Ryan Griffith on 17 May, 2016 07:52 PM

    Ryan Griffith's Avatar

    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. 2 Posted by Rob Knight on 18 May, 2016 02:49 PM

    Rob Knight's Avatar

    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. 3 Posted by Ryan Griffith on 18 May, 2016 03:55 PM

    Ryan Griffith's Avatar

    Hi Rob,

    Velocity would be the easier option. Consider the following snippet to help get you started:

    #foreach ($asset in $assets)
      #set ($path = $asset.getChild("path").value)
      #set ($depth = $path.split("/").size())
    #end
    

    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:

    #set ($depth = $_MathTool.sub($path.split("/").size(), 1))
    

    Please let me know if you have any questions.

    Thanks!

  4. 4 Posted by Rob Knight on 05 Jun, 2016 05:46 AM

    Rob Knight's Avatar

    This was extremely helpful. Thank you, Ryan.

  5. 5 Posted by Ryan Griffith on 06 Jun, 2016 12:02 PM

    Ryan Griffith's Avatar

    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!

  6. Ryan Griffith closed this discussion on 06 Jun, 2016 12:02 PM.

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