Umbraco Version = 6.0.3
I'm trying to do some seemingly simple stuff in a macro scriptlet. Basically, I want to loop through all of the visible child content that is not a category:
#inherits umbraco.MacroEngines.DynamicNodeContext
#{
var subs = Model.Children.Where("Visible && DocumentTypeAlias != \"Category\"");
}
<span>Count: #subs.Count()</span>
#if (subs.Any())
{
<ul>
#foreach (var sub in subs)
{
<li>
#sub.Name
</li>
}
</ul>
}
If I take out the "Visible" portion of the where clause, it works correctly (with the exception of displaying content marked as hidden). I can also use "Visible" on it's own by removing the "DocumentTypeAlias", but then all visible content including categories are displayed.
I also tried using strongly typed queries #Model.Content.Children.Where(x => x.IsVisible() && x.DocumentTypeAlias != "Category") but I get an error about not being able to use lambda functions with dynamically typed content.
Ideas?
Two things:
The DynamicNode Where clause uses a parameter syntax.
Use NodeTypeAlias to check the document type.
Example:
var subs = Model.Children.Where("Visible && NodeTypeAlias != #0", "Category");
Here are a few Umbraco razor resources:
Umbraco 4.7 Razor Feature Walkthrough. It's a 8-part series and it's pretty informative. Don't be put off by the version reference, it's still valid for Umbraco 6.
Razor DynamicNode Cheat Sheet.
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 just recently upgraded Umbraco from 4.7.2 to 7.1.9 and now in the process of slowly converting all legacy macroscripts to partial view macros. I have come across a few issues when using DescendantsOrSelf to iterate through nodes.
I have a macro that generates the side menu for my site (intranet). With version 4 the macro worked as expected on the whole site displaying the appropriate menu on the homepage and different side menu's on the child pages.
After the upgrade the below condition:
var model = GetParentSideMenu(CurrentPage);
#if (CurrentPage.AncestorsOrSelf("umbSomePageType").Where("Visible").First().HasValue("PageName") && CurrentPage.AncestorsOrSelf("umbSomePageType").Where("Visible").First().Id != model.Id)
{ ... }
#functions
{
public dynamic GetParentSideMenu(dynamic model)
{
if (model.Level > 1)
{
do
{
if (model.umbSideMenuLinks.Count() > 0)
{
return model;
}
if (model.Level > 1) {
model = model.Up();
} else { break; }
} while (model.Up() != null);
return model;
}
else return model;
}
}
Generates the following error when rendered on the homepage:
System.InvalidOperationException {"Sequence contains no elements"}
Inner Exception is null
The understanding here is that the page being rendered is not "umbSomePageType" so this condition should be false and move on but instead it throws the above exception.
The macro works fine when rendered on a "umbSomePageType" page but as the user is allowed to have further sub pages of a another type under "umbSomePageType" I have to manually check the "DocumentTypeAlias" and make sure the other if statements checking for that type of sub page are not executed because I get the same error as above.
Another issue I am facing is this doesn't return an iterable collection when it used to before in v4:
#if (model.DescendantsOrSelf("umbSideMenuLinks").Where("Visible").Count() > 0)
{
foreach (var item in model.DescendantsOrSelf("umbSideMenuLinks").Where("Visible").First().Children)
{ ... }
.... }
The if condition returns true but the foreach is unable to get any elements to iterate through.
Any help here will be greatly appreciated.
Thank You.
I was able to find a solution to the first problem I specified above. The error was occurring in the "if" condition due to the "CurrentPage" being a dynamic object, "FirstOrDefault()" needs to be used instead of "First()".
The second issue still stands and to further detail that issue if you have the following directory structure:
Root Folder/Homepage
- Second Level Folder
-- SideMenuLinks (umbSideMenuLinks)
-- SomePage...
- SideMenuLinks (umbSideMenuLinks)
In the above case if I am on the "HomePage" and I want to render the "SideMenuLinks" pertaining to the homepage and I use "DecedantOrSelf" it will go from the root folder to the second level folder and to the "SideMenuLinks" instead of first checking it "Self" which is the behaviour I desire. This is something that worked in Umbraco v4 but in v7 it drills into the sub directories first.
Maybe if I reorder/sort the SideMenuLinks so they appear before the "Second Level Folder" ?
Haven't tried that yet.
UPDATE: So I just tried the sorting and if I sort the tree this way:
Root Folder/Homepage
- SideMenuLinks (umbSideMenuLinks)
- Second Level Folder
-- SideMenuLinks (umbSideMenuLinks)
-- SomePage...
It starts to work. In the future we will have to create the page structure this to make sure that the correct SideMenuLinks are hit.
We are building a website with the Orchard CMS where we have campaign adds.
These adds are linked to pages through tags (not the orchard tag part).
Then we built a custom filter that take these tags into consideration when fetching the campaign adds and display them in a widget.
On some pages there are tags but no campaigns that match these tags. We would like to hide the widget at this point.
One solution is to edit the widget layer every time a new campaign is added. But I would like to have a more solid solution than this.
Summary:
We would like to hide the entire projection widget when the query returns an empty result.
// Madelene
Most of the markup in the widget is rendered by the Widget.Wrapper.cshtml template. What you can do is filter what this wrapper will render based on the content of the widget itself. This way if the widget doesn't render anything, you can decide to hide the title and the other zones. Here is the code doing it:
#using Orchard.ContentManagement;
#using Orchard.Widgets.Models;
#{
var widgetPart = ((IContent)Model.ContentItem).As<WidgetPart>();
var tag = Tag(Model, "article");
var childContent = Display(Model.Child);
}
#if (!String.IsNullOrEmpty(Convert.ToString(childContent))) {
#tag.StartElement
if ((widgetPart.RenderTitle && HasText(widgetPart.Title)) || Model.Header != null) {
<header>
#if ((widgetPart.RenderTitle && HasText(widgetPart.Title))) {
<h1>#widgetPart.Title</h1>
}
#Display(Model.Header)
</header>
}
#childContent
if (Model.Footer != null) {
<footer>
#Display(Model.Footer)
</footer>
}
#tag.EndElement
}
Just create a file named Widget.Wrapper.cshtml in your theme and paste this content. You can check what was the previous content if you want to understand how it's done.
you could override the Projection widget, you can then edit the code for that widget within the cshtml file. This is by far the easiest way.
or
You could create a custom filter that did a check for the dependency and returned no items if that dependency was not met (this is a harder way of doing it)