Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • marie 22 posts 132 karma points
    Jul 03, 2018 @ 18:19
    marie
    0

    Is it possible to convert an integer to a value that can be "foreach"ed through?

    Using razor, is it possible to convert a single-digit value to something that can dynamically generate content on a page?

    For example, I want to show an image x number of times based off a value inputed in an Umbraco field. If the number "3" is typed into the field, I want to show the image 3 times. If the number "5" is typed, 5 times, and so on.

    I've done a bit of googling and haven't found anything that gives me the correct results, i.e., something that cleanly translates the number "5" into "show this content 5 times".

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jul 03, 2018 @ 19:01
    Jan Skovgaard
    100

    Hi Marie

    You should be able to use a for loop like this

    @for(var i = 1; i <= 5; i++)
    {
         <p>Line @i</p>
    }
    

    This example will render 5 lines saying "Line 1", "Line 2" etc.

    In the above you will of course need to replace the paragraph with your HTML for the image rendering and where it says 5 you will need to render the number value from your umbraco document type.

    I hope this example makes sense and is enough to get you going? :)

    /Jan

  • marie 22 posts 132 karma points
    Jul 03, 2018 @ 20:18
    marie
    0

    That works perfectly, Jan! Thanks so much!

Please Sign in or register to post replies

Write your reply to:

Draft