Copied to clipboard

Flag this post as spam?

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


  • Anders Brohus 194 posts 475 karma points
    Jun 28, 2014 @ 23:20
    Anders Brohus
    0

    Get random pictures from media folder

    Hi!

    I'm developing a website right now, where there is an Image Carousel all the time but right now they are hardcoded but i can't figure out how to make some razor code that goes into my image folder and take 10 random images and it should do it at each page refresh or change.. :)

    My media folder has the ID of 1064 and the name is "Image Carousel", and the output HTML should be like this

    <div>
         <a href="PATH TO IMAGE" title="IMAGE NAME" data-hover="IMAGE NAME" data-toggle="fancybox" class="fancybox">
             <img src="PATH TO IMAGE" alt="IMAGE NAME" />
         </a>
    </div>
    

    Can anyone help me with making this razor code?

    I don't know why i can't figure it out ... Because it should be quite simpel.. xD

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 29, 2014 @ 08:05
    Jeavon Leopold
    102

    Hi Anders,

    I think this should do it:

    @{
        var randomImages = Umbraco.TypedMedia(1064).Children.RandomOrder().Take(10);
        foreach (var image in randomImages)
        {
            <div>
                <a href="@image.Url" title="@image.Name" data-hover="@image.Name" data-toggle="fancybox" class="fancybox">
                    <img src="@image.Url" alt="@image.Name" />
                </a>
            </div>
        }
    }
    

    Jeavon

  • Anders Brohus 194 posts 475 karma points
    Jun 29, 2014 @ 16:04
    Anders Brohus
    0

    Thank you so much Jeavon! :)

    It works really good, and looks so great in the code :D

  • Anders Brohus 194 posts 475 karma points
    Jun 29, 2014 @ 23:09
    Anders Brohus
    0

    Jeavon, what if we made it an into a razor macro, with an option to hide it?

    Because each page got an True/False, named doNotDisplayImageCarousel, and if it's checked it should not display it, but if it's not checked it displays it?

    Sorry for asking for so much help but i'm relatively new into razor programming :)

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 30, 2014 @ 00:03
    Jeavon Leopold
    0

    Hi Anders,

    If the page has the True/False rather than it being a macro parameter in the template then why need a macro?

    How about this:

    @{
        if (!Model.Content.GetPropertyValue<bool>("doNotDisplayImageCarousel")){
        var randomImages = Umbraco.TypedMedia(1064).Children.RandomOrder().Take(10);
        foreach (var image in randomImages)
        {
            <div>
                <a href="@image.Url" title="@image.Name" data-hover="@image.Name" data-toggle="fancybox" class="fancybox">
                    <img src="@image.Url" alt="@image.Name" />
                </a>
            </div>
        }
    }
    }
    

    Please don't hesitate to ask anything further :-)

    Jeavon

    P.S Razor Macros are now legacy, if you do need a macro then you should use a Partial View Macro (but I don't think you need a macro anyway)

  • Anders Brohus 194 posts 475 karma points
    Jun 30, 2014 @ 14:14
    Anders Brohus
    0

    Okay thanks you! :)

    But a question can you tell me what the difference is between a Partial View and a Razor Macro?

    I studied Web Development, where i learned Razor but no MVC at all.

    So this looks up if the page has an check in doNotDisplayImageCarousel and if it has then it won't display the Image Carousel? :)

    EDIT:

    And if you should make the ID of the media folder dynamic how should i do that?

    Because it's a website for my mom and dad's dogs, and on every page i want that Carousel to be there but when i'm on a specific dog i want the pictures to be taken from a folder that changes for each dog :)

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 30, 2014 @ 15:19
    Jeavon Leopold
    0

    You're welcome!

    Ok, in very basic terms, a Partial View is like a chunk of Razor logic that you can reuse, it is native to MVC. A Partial View Macro is a Partial View wrapped by Umbracos Macro engine so that you can use it with WebForms or when you need to pass in parameters from the RTE editor. A Razor Macro is the legacy implementation of using Razor for use with WebForms, it shouldn't be used any more.

    On your specific question, I would recommend that you create a media picker on your content page that picks the media folder that contains your images. This will also mean you won't need doNotDisplayImageCarousel as if the picker is empty we can assume to not display the Carousel.

    It would look like this:

    @{
        if (Model.Content.HasValue("myMediaFolderPickerAlias"))
        {
            var myMediaFolderPickerId = Model.Content.GetPropertyValue<int>("myMediaFolderPickerAlias");
            var randomImages = Umbraco.TypedMedia(myMediaFolderPickerId).Children.RandomOrder().Take(10);
            foreach (var image in randomImages)
            {
                <div>
                    <a href="@image.Url" title="@image.Name" data-hover="@image.Name" data-toggle="fancybox" class="fancybox">
                        <img src="@image.Url" alt="@image.Name" />
                    </a>
                </div>
            }
        }
    }
    
  • Anders Brohus 194 posts 475 karma points
    Jun 30, 2014 @ 18:38
    Anders Brohus
    0

    Arha okay nice thanks you :)

    So you would not make use of either Razor Scripts or Partial Views, but instead just write the code in the template? :)

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 30, 2014 @ 18:46
    Jeavon Leopold
    0

    No worries. Generally, I would use only Views (templates) and Partial Views for reusable code.

  • Anders Brohus 194 posts 475 karma points
    Jun 30, 2014 @ 19:00
    Anders Brohus
    0

    Okay :)

    Do you have any pages where i can read about the "inline code" like the one you created for me? :)

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jun 30, 2014 @ 21:16
    Jeavon Leopold
    0

    Have you watched to free Umbraco.tv v7 videos? http://umbraco.tv/videos/umbraco-v7/ There is a chapter called querying data with Razor...

  • Anders Brohus 194 posts 475 karma points
    Jun 30, 2014 @ 21:35
    Anders Brohus
    0

    Nope haven't used Umbraco.TV, i will watch it right away! :D

Please Sign in or register to post replies

Write your reply to:

Draft