Copied to clipboard

Flag this post as spam?

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


  • kyle 49 posts 240 karma points
    Dec 09, 2021 @ 07:54
    kyle
    0

    rendering content from different doctype

    Hello everyone,

    I've been searching the web but did not find anything so this is my last resort...

    I want to be able to render content in a template from a different doctype

    so for example let's say I have a doctype named "Home" and its corresponding template, to render I would say

    @Model.Value("alias")
    

    I want to know if a can do that, but render from another page, so for example in my Home template render content from the "contact us page"

    something like(in-home template):

    @contact.alias
    

    I hope this makes sense, and thank you in advance

  • Frank Laumann 39 posts 303 karma points
    Dec 09, 2021 @ 09:26
    Frank Laumann
    0

    Hi Kyle

    This is very much possible. It is hard to explain how without knowing your project structure.

    But I think my best solution would be if you want to render data from a node that is the only node of a certain doctype that could be named contactPage, I would do something a like this:

        @{
            var contactPage = Model.Root().DescendantsOrSelf().FirstOrDefault(x => x.ContentType.Alias == "contactPage");
        }
    @contactPage.Value("alias")
    

    You can even make it typed with

    FirstOrDefault<ContactPage>(... 
    

    to be able to use contactPage.Alias

    I hope you will be able to solve your problem now.

    Best regards Frank

  • kyle 49 posts 240 karma points
    Dec 09, 2021 @ 14:33
    kyle
    0

    Hi Frank, thanks for the reply,

    my situation is strange because I know what the problem is, but how to solve it...

    I have a master template with a navbar and footer, they render text content like this:

    @Model.Value("alias")
    

    from a "home page" doctype

    that home page doctype has its own template that inherits from the master

    and my contact page is the same, doctype with a template that inherits from the master, but now when a different page gets rendered, it runs through the master template and finds that the specified aliases are not in the doctype, because it is obviously in the home page doctype...

    This may seem very confusing, but my goal is just to have the navbar and footer content be rendered

  • Frank Laumann 39 posts 303 karma points
    Dec 09, 2021 @ 16:44
    Frank Laumann
    100

    Hi Kyle

    Okay I think I understand. So you need to get the model with the values for the navbar. If it is the root node, you can just use:

    var rootModel = Model.Root();
    

    If it is not the root node, you need to find it by doctype, like my previous answer:

    var navbarModel= Model.Root().DescendantsOrSelf().FirstOrDefault(x => x.ContentType.Alias == "homePage");
    

    If you have navbar and footer in seperate partial view files, you can render them like this:

    @Html.Partial("Navbar", navbarModel)
    

    or like this:

    @{
        Html.RenderPartial("Navbar", navbarModel);
    }
    

    I hope that makes good sense to you.

    Best regards Frank

  • kyle 49 posts 240 karma points
    Dec 10, 2021 @ 06:13
    kyle
    0

    Thank you so much!

    var rootModel = Model.Root()
    
    <p>@rootModel.Value("alias")</p>
    

    did the trick for me

Please Sign in or register to post replies

Write your reply to:

Draft