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, 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 :)
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 :)
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>
}
}
}
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
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
Hi Anders,
I think this should do it:
Jeavon
Thank you so much Jeavon! :)
It works really good, and looks so great in the code :D
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 :)
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:
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)
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 :)
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:
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? :)
No worries. Generally, I would use only Views (templates) and Partial Views for reusable code.
Okay :)
Do you have any pages where i can read about the "inline code" like the one you created for me? :)
Have you watched to free Umbraco.tv v7 videos? http://umbraco.tv/videos/umbraco-v7/ There is a chapter called querying data with Razor...
Nope haven't used Umbraco.TV, i will watch it right away! :D
is working on a reply...