Copied to clipboard

Flag this post as spam?

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


  • Ren 8 posts 89 karma points
    Jul 16, 2016 @ 11:10
    Ren
    0

    How do I display contents of doc type 'A' in a template not linked with doc type 'A', but linked doc type 'B'?

    Hello all,

    I'm new to Umbraco and I am really struggling to understand various concepts and I hoped someone could help me out.

    The issue I am currently having is how to display properties from document type one (openingTimes), or its child nodes (openingTimesDays), and display them in a template (Haircuts.cshtml) which is not "linked" to doc type one (openingTimes)

    It may make more sense if i describe the issue in connection with the picture.

    I have a 'Haircuts' template, which presents information about various haircuts. The information presented in this template is taken from the 'Haircuts' child nodes - Gents, Ladies, Wash and Blow dry etc.

    I have created another doc type (openingTimes) which has a number of child nodes below that - Monday, Tuesday etc (openingtimesDays)enter image description here.

    How do I display the properties of the Monday, Tuesday etc. in the Haircuts template?

    As I understand it, every item in the tree is linked at root and I need to traverse the tree, but I don't know how to do it.

    enter image description here

  • Marc Goodson 2157 posts 14434 karma points MVP 9x c-trib
    Jul 17, 2016 @ 11:57
    Marc Goodson
    1

    Hi Ren

    It depends on which approach you are using for your templates, dynamic, strongly typed models or IPublished content.

    But the principle is the same.

    eg. from the Haircuts document type, you need to be able to find your 'openingTimes' document type, and then list out it's children.

    You can get access to the root node of your site from within your template by using any of the following:

    var [email protected](1);
    
    var rootByDynamic = @CurrentPage.Site();
    
    var rootByTypedContent = @Umbraco.TypedContentAtRoot().FirstOrDefault();
    

    Once you have your root element, you can then look for descendants of type 'openingTimes'

    //using IPublishedContent
    var openingTimesByDescendants = rootByTraversing.Descendants().Where(f=>f.DocumentTypeAlias == "openingTimes").FirstOrDefault();
    
    // using dynamic
    var openingTimesByDynamicDescendants = rootByDynamic.Descendants().Where("DocumentTypeAlias == \"openingTimes\"").First();
    
    //using Xpath
    var openingTimesByXPath = Umbraco.TypedContentAtXPath("//openingTimes").FirstOrDefault();
    

    Then when you have your openingTimes node you can list out the openingTimesDays elements as they are children of the openingTimes docType: eg something like this:

    foreach (var otDay in openingTimesByDescendants.Children()){
    <li>@otDay.GetPropertyValue<string>("day") - @otDay.GetPropertyValue<string>("openingTime") - @otDay.GetPropertyValue<string>("closingTime")</li>
    }
    

    or in dynamics

    foreach (var otDay in openingTimesByDescendants.Children()){
    <li>@otDay.day - @otDay.openingTime -@ otDay.closingTime</li>
    }
    

    More info here: https://our.umbraco.org/documentation/Reference/Querying/ and https://our.umbraco.org/Documentation/Reference/Templating/Mvc/querying

  • Ren 8 posts 89 karma points
    Jul 20, 2016 @ 17:55
    Ren
    0

    Hello Marc,

    Sorry, a quick questions about this.

    var [email protected](1);

    is the Models approach and I assume:

    //using IPublishedContent var openingTimesByDescendants = rootByTraversing.Descendants().Where(f=>f.DocumentTypeAlias == "openingTimes").FirstOrDefault();

    is the 'Model' approach to look for decendants. Am I correct ?

    Also, is the 'f' in f=>f.DocumentTypeAlias == "openingTimes" an arbitrary letter? Could it be p or b or any other letter?

    I ask about the letter 'f' because I received a red squiggle in Visual Studio with the error being 'Cannot convert lamba expression to type 'string' because it is not a delegate type'

    The Dynamic approach obviously work.

    Regards, Parm.

  • Ren 8 posts 89 karma points
    Jul 18, 2016 @ 20:55
    Ren
    0

    Hello Marc,

    Result! Thank you so much.

    I read, and re-read, the page you provided before posting, hoping to understand it, but it was tough.

    I still struggle with what to use: dynamic, strongly typed models or IPublished content. As I don't fully understand the differences.

    I will continue to work through the documentation. I'm sure (hope) it will click!

    Many thanks.

  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    Jul 18, 2016 @ 21:27
    Alex Skrypnyk
    1

    Hi Ren,

    Welcome to our forum!

    Definitely do not use dynamics!

    The best way if you are using visual studio is strongly typed models.

    Thanks,

    Alex

  • Ren 8 posts 89 karma points
    Jul 19, 2016 @ 17:21
    Ren
    0

    Hello Alex,

    Cool, thanks for the advice. I will stick with Models.

    Regards, Ren

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies