Copied to clipboard

Flag this post as spam?

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


  • Gavin Williams 54 posts 247 karma points
    Sep 06, 2017 @ 12:33
    Gavin Williams
    0

    Passing vars from base template to child template

    Interesting scenario that I haven't come across before. Say for example I have a "Base" template that sets some top content variable like so:

    var topContent = Model.Content.Children().FirstOrDefault(x => x.DocumentTypeAlias == "TopContent");

    The base template renders some page areas with reference to this topContent var.

    Then I have a "Child" template whose layout is set to "Base", this child template also has the same line of code to set the top content and render some different content.

    So, during the page render process does the system cache the base template topContent result and just re-use it from Child, or does it result in two trips to the database (or content cache)? Assuming for the moment that it results in two trips, what is the best way to make variables from a base template available to templates that inherit from it? The simplest way I can think of is to set and use ViewData from the base template but I don't like that as a solution, it's not very elegant.

    So, thought I'd throw this one out to the community for comment. Any thoughts?

  • Laurent Lequenne 123 posts 248 karma points
    Sep 07, 2017 @ 07:48
    Laurent Lequenne
    0

    You should use a specific model that you generate only once in your controller with the advantage that you run the queries only once, if you need to access multiple time properties in your view there are parsed only when you create your model, and not each time you access a property of your IPublishedContent.

Please Sign in or register to post replies

Write your reply to:

Draft