Loop through numbered fields


If you have multiple fields or groups of fields that are similar (i.e. "Image 1," "Image 1 Caption," "Image 2," "Image 2 Caption"), you should write your velocity code once to put them into your page instead of repeating yourself for each one. You can do this with a combination of a "foreach" loop, and a "get()" with the name of the field.


${esc.hash}foreach(${esc.dollar}thing in ${esc.dollar}dotcontent.pull("+structureName:EbiblestudyLesson",10,"modDate desc"))
        ${esc.hash}foreach(${esc.dollar}num in [1..3])
                ${esc.hash}set(${esc.dollar}fieldname = "worksheet${esc.dollar}!{num}")

Have you created code you'd like to share with the dotCMS community?

Share your code Become a Contributor