I am new to Umbraco. I am creating an image gallery (called Customers). A Customer has a logo, which is an image.
How do I create a razor macro that outputs a list of customer logos?
I am after the cshtml code, probably something like this:
#inherits umbraco.MacroEngines.DynamicNodeContext
#foreach (var customer in Content.Customers) {
<img src="#customer.logo.umbracoFile" alt="#customer.Name"/>
}
Thanks in advance!
I'm assuming the script executes on the Customers page, and that the content type containing log and url is called Customer:
#foreach(var customer in Model.Customer)
{
<img src="#customer.Media("logo", "umbracoFile")" alt="#customer.Name"/>
}
Model.Customer will give you a list of all children of the current page which are Customers (content type).
Here is the code that works
#inherits umbraco.MacroEngines.DynamicNodeContext
#foreach (var customer in Model.NodeById(1062).Children) {
<img src="#umbraco.IO.IOHelper.ResolveUrl(customer.logo)" alt="#customer.Name"/>
}
for using a dynamic location (macro parameters), you could do two things:
1. setting this in your root document so the user can change the customer slider location (if you would like that):
var RootNode = #Model.NodeById(#Model.AncestorOrSelf(1).HeaderRoot);
Where HeaderRoot is the name of a property on your starting file (root)
2. Using a parameter on your razor script
var rootNode = #Parameter.RootNode;
and use it something like:
#Model.NodeById(rootNode).Children();
so in your code, it would look something like this (not tested):
#inherits umbraco.MacroEngines.DynamicNodeContext
#{ var rootNode = #Parameter.RootNode;}
#foreach (var customer in Model.NodeById(rootNode).Children()) {
<img src="#umbraco.IO.IOHelper.ResolveUrl(customer.logo)" alt="#customer.Name"/>
}
Related
2SXC 10.25.2 / DNN 9.3.2
I have a 2sxc module that uses a C# template with "list" enabled. I have a content type called "pathway" and inside that I have 2 entity picker fields for "first step sessions" and then "next step sessions". These entity pickers use a "session" content type. Inside each of those "session" content types I also have an entity picker for "speaker(s)". All in all, it's a setup that I have lists within lists within lists.
When I create the loops for each of the sublists, I can easily do that within the 1 C# template but it becomes repetitive, long, and unruly because there's so much c# code where I'm looping the same session template for different entity picker sections. So, I tried using the "Render sub-template" code to simplify the template - I created new sub templates and inserted them in - it seemed to work at first, however, the template started outputting all "session" items into each item in the list.
I suspect that the subtemplate somehow loses the context of the item that it's in so it's outputting everything. Is there something special I need to know about using subtemplates with for each loops? Do I have to include params and, if so, how do I do that?
EDIT to include code sample:
Here is a small, simplified version of the code I'm working with:
#foreach(var Content in AsList(Data)) {
<h2>#Content.Title</h2>
<h3>Lead Sessions</h3>
<div class="lead-sessions text-green">
#foreach(var item in AsList(Content.LeadSessions as object)){
<h4>#item.LeadSessionTitle</h4>
<p>#item.LeadSessionText</p>
}
</div>
<h3>Next Sessions</h3>
<div class="next-sessions text-green">
#foreach(var nextitem in AsList(Content.NextSessions as object)){
<h4>#nextitem.LeadSessionTitle</h4>
<p>#nextitem.LeadSessionText</p>
}
</div>
}
I want to make a subtemplate so I don't have to repeat the same code for the sessions loop. How could I simplify this template to use a subtemplate for looping the sessions within the lead-sessions and next-sessions?
So based on the modified question, it's a bit like this
#foreach(var Content in AsList(Data)) {
<h2>#Content.Title</h2>
<h3>Lead Sessions</h3>
#RenderPage("_inner.cshtml", new { Items = Content.LeadSessions })
<h3>Next Sessions</h3>
#RenderPage("_inner.cshtml", new { Items = Content.NextSessions })
}
Second file _inner.cshtml
#{
var items = AsList(PageData["Items"]);
}
<div class="next-sessions text-green">
#foreach(var nextitem in items){
<h4>#nextitem.LeadSessionTitle</h4>
<p>#nextitem.LeadSessionText</p>
}
</div>
Yep, you can just use RenderPage without params, or pass in params like in the blog app:
#RenderPage("shared/_Category Filter.cshtml", new { MobileView = true, FilteredCategory = filteredCategory })
See https://github.com/2sic/app-blog/blob/master/_List.cshtml#L25
Then the template can retrieve the values like
#{
var filteredCategory = PageData["FilteredCategory"];
}
See https://github.com/2sic/app-blog/blob/master/shared/_Category%20Filter.cshtml#L6
You can pass around any amount of values/objects like this.
You can also create helpers - and then call those helpers. Like this
https://github.com/2sic/app-news/blob/master/shared/_Helpers.cshtml#L24-L33
I am currently trying to output data from the new Multinode Treepicker in Umbraco 7.6.4 here is my current setup and code:
Doctype
Content Node with 'Page' Doctype
Code to output the names of the selected nodes:
#{
IPublishedContent typedContentPicker = Model.Content.GetPropertyValue<IPublishedContent>("sections");
if (typedContentPicker != null)
{
<p>#typedContentPicker.Name</p>
}
}
This I took from the official Umbraco Documentation and adapted it to my project. This code is in a template with the 'page' doctype.
Currently the above code does not output anything to my page, I am expecting to see a list of nodes displayed on the page, can anyone see what the issue is or where I am going wrong?
Dumb moment, I was looking at the documentation for a content picker and not a multi-node tree picker!
Correct code is:
#{
var typedMultiNodeTreePicker = Model.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("sections");
foreach (var item in typedMultiNodeTreePicker)
{
<p>#item.Name</p>
}
}
I have a Razor partial which displays my site navigation:
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
var home = CurrentPage.Site();
umbraco.NodeFactory.Node navigationSettingsNode = MySite.Umbraco.NavigationSettings;
dynamic navigationSettings = new umbraco.MacroEngines.DynamicNode(navigationSettingsNode.Id);
var settings = home.Children.Where("DocumentTypeAlias == \"Settings\"").First();
}
#if (navigationSettings.HasValue("topNavigation"))
{
<ul>
dynamic topNavigation = navigationSettings.topNavigation;
var topNavigation2 = settings.topNavigation;
<span>#topNavigation</span>
<span>#topNavigation2</span>
foreach(dynamic item in topNavigation)
{
<li>
#item.caption
</li>
}
</ul>
}
Initially I was looping through topNavigation2 items which worked fine and with no problem.
Now I'm looping through topNavigation items and it throws an error:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'char' does not contain a definition for 'link'
I don't want to use var settings anymore, I want to use only dynamic navigationSettings variable. In order to get the right node of navigationSettings I need to some operation and I don't fancy to paste the same code in every view I want to use it so I want it to be accessible from dll and available to use anywhere.
Also the navigationSettings node in my Umbraco is outside of main content tree so is not a child of Home.
Why isn't it working? Both
dynamic topNavigation = navigationSettings.topNavigation;
var topNavigation2 = settings.topNavigation;
produce the same json result and both are dynamic objects.
How to make it work correctly?
I'm using MVC 5.2.3
It looks like your topNavigation property is a string, and so when you call for each on it, it's iterating through the characters in the string.
Also, don't use NodeFactory, it's deprecated. You should be using IPublishedContent instead.
I'd use the strongly typed content objects instead of dynamic, as a) they're faster, and b) they're easier to work with.
Here's a great article explaining the different ways of getting content: https://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/
I am new to Umbraco. I am currently using Umbraco 7.0. I have created a Razor
macro with a parameter that will access property from home template page. In My Macro
due to space only first word of string displays and rest of the string is not
displayed. property
name is sliderHeading with value : My First Slide. Macro inserted in the template is
as follows :-
#Umbraco.RenderMacro("HomePageSlider", new {mediaId="1084",
sliderHeading=#Umbraco.Field("sliderHeading")})
// where sliderHeading is macro parameter.
Macro definition :-
#inherits umbraco.MacroEngines.DynamicNodeContext
#if (Parameter.mediaId != null)
{
var mediaFolder = Library.MediaById(Parameter.mediaId);
if (mediaFolder.Children.Any())
{
foreach (var mediaItem in mediaFolder.Children)
{
<div class="cycle-slide" style="background-
image:url(#mediaItem.umbracoFile)" data-cycle-title=#Parameter.sliderHeading
data-cycle-desc="Remember, you are not limited to image elements. You can display
other HTML too." data-cycle-link="#"></div>
}
}
}
So, in macro, parameter value is accessed by :- data-cycle-
title=#Parameter.sliderHeading.
Now my issue is that
if property value is :- "My First Slide". Then it will give wrong output "My" on page.
if property value is :- "My First Slide" with html space (&nbbsp;) in between Then it
will give right output "My First Slide" on page.
How should I access parameter in Macro, so that it will give right output.
Seems a bit simple but won't wrapping #Html.Raw or even #Raw round your code force the parameter to output with spaces?
Like:
data-cycle-title=#Raw(Parameter.sliderHeading)
I have a Taxonomy called Categories. In this taxonomy I have multiple levels of terms. Each term is a CategoriesTerm contenttype.
I have defined a custom razor template: Content-CategoriesTerm.cshtml, which is used when I click a CategoriesTerm on the frontend.
Now I want to display on that page all direct children (if any), but there is no property like Term.Children or something. How can I get the children of a term in the Razor view?
Check Taxonomy.cshtml and Orchard.Mvc.DisplayChildren. It goes like this:
var tag = Tag(Model, "ul");
IList<dynamic> items = Model.Items;
if (items.Any()) {
items[0].Classes.Add("first");
items[items.Count - 1].Classes.Add("last");
<div>
#tag.StartElement
#* see MenuItem shape template *#
#DisplayChildren(Model)
#tag.EndElement
</div>
#DisplayChildren could be done in Razor if you like:
public IHtmlString DisplayChildren(dynamic shape) {
var writer = new HtmlStringWriter();
foreach (var item in shape) {
writer.Write(Display(item));
}
return writer;
}