We created a content type named NewsArticles) that has our required fields (Title, SubTitle, Description and Date). Note: When we created the content type (NewsArticles) we also created the matching template.
In our site we have 5 places where we have created content based on NewsArticles.
I now need some generic Razor logic to loop through each site's NewsArticles and display on a screen. I also need to know how to convert my template to a partial view.
As I am new to Razor and Umbraco, can somehow point me in the right direction?
You need to use your news items 5 differeent places on your website, then you can create a partial view or a patial view macro and inserted it on the template where the news items should appears.
I have made some razor code that should point you in the right direction, depeding on how your content / site structure is. But here is the code that you should you for getting news items that you can reuse on the your site.
@{ @* Get the root of the website *@ var root = CurrentPage.AncestorOrSelf(1).Descendants("umbNewsOverview").FirstOrDefault(); }
I will try to explain what the code does, it will go up to the first level on your site, and then search the tree structure to find the first item with the document type alias of umbNewsOverview, and the pull out all it´s children. So depeding on your structure, then you need to change the umbNewsOverview, so it match with your alias for the document type that you are using for your overview page for your news section.
If you are using Umbraco 7.2, then you have a query builder that can help you buliding your Razor code. When you are in a partial view, parial view macro or your in template, then press the button at the right on the ribbon. If you use the query builder the code would look something like this.
@{ var root = CurrentPage.Site().FirstChild("umbNewsOverview");
News Example
We created a content type named NewsArticles) that has our required fields (Title, SubTitle, Description and Date).
Note: When we created the content type (NewsArticles) we also created the matching template.
In our site we have 5 places where we have created content based on NewsArticles.
I now need some generic Razor logic to loop through each site's NewsArticles and display on a screen.
I also need to know how to convert my template to a partial view.
As I am new to Razor and Umbraco, can somehow point me in the right direction?
Thanks
Tom
Hi Tom,
You need to use your news items 5 differeent places on your website, then you can create a partial view or a patial view macro and inserted it on the template where the news items should appears.
I have made some razor code that should point you in the right direction, depeding on how your content / site structure is. But here is the code that you should you for getting news items that you can reuse on the your site.
I will try to explain what the code does, it will go up to the first level on your site, and then search the tree structure to find the first item with the document type alias of umbNewsOverview, and the pull out all it´s children. So depeding on your structure, then you need to change the umbNewsOverview, so it match with your alias for the document type that you are using for your overview page for your news section.
If you are using Umbraco 7.2, then you have a query builder that can help you buliding your Razor code. When you are in a partial view, parial view macro or your in template, then press the button at the right on the ribbon. If you use the query builder the code would look something like this.
If you have access to Umbraco TV, there is two chapters about Razor http://umbraco.tv/videos/implementor/working-with-umbraco-data/razor-syntax/introduction-to-razor/ and http://umbraco.tv/videos/implementor/working-with-umbraco-data/querying-umbraco-data-with-razor/
Here is also some documentation on Razor https://our.umbraco.org/documentation/Using-Umbraco/Creating-Basic-Site/Articles-Parent-and-Article-Items
Hope this make sense and helps. If you have any questions to this don't hesitate to ask again.
/Dennis
is working on a reply...