Copied to clipboard

Flag this post as spam?

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


  • Brent Jubinville 7 posts 27 karma points
    Dec 12, 2011 @ 18:52
    Brent Jubinville
    0

    Shared Content With Razor

    Hello,

    I'm lookin for a little sanity check with a implementation of shared content within a site.  The site I'm building has several regions with largely the same content throughout, but some region specific pages and copy as well.

    From the root node, I have:

    • Shared Content
      • Page 1 Content (Has Actual Content)
    • Region #1
      • Page 1 Content (Content Picker to Shared Content)
    • Region #2
      • Page 1 Content (Content Picker to Shared Content)

     

    To select the shared content I'm using a content picker on a master document type.  In my master template, I have a razor macro called ContentRenderer:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @if (Model.HasValue("UseContentFrom"))
    {
        var sharedContent = Library.NodeById(Model.UseContentFrom);
        if (Model.NodeTypeAlias == sharedContent.NodeTypeAlias)
        {
            umbraco.library.RenderTemplate(sharedContent.Id);
        }
        else
        {
        <div class="row">
            <div class="twelve columns">
                <p style="color: Red;">
                    The document type of the content your trying to use doesn't match the document type
                    of your page.
                </p>
                <p>
                    Page Document Type: <strong>@Model.NodeTypeAlias</strong>
                    <br />
                    Shared Content Document Type: <strong>@sharedContent.NodeTypeAlias</strong>
                </p>
            </div>
        </div>
        }
    }
    

    Of note, is the call to RenderTemplate.  Provided the document types match (which I check for), as soon as RenderTemplate is called, the Model node is effectively replaced with that of the shared content.  This is great since I don't have to define new templates, or do any custom coding to spit out shared data.  The downside is it is a little cumbersome to manage through the UI, and links etc end up pointing back to the shared content folder.

    I think this is pretty good solution given my requirement, but I'd love to hear from the community to see if there is a better way to achieve this.

     

  • Rodion Novoselov 694 posts 859 karma points
    Dec 13, 2011 @ 18:32
    Rodion Novoselov
    0

    Hi, Brent. There's also an excellent article in wiki that may interest you: http://our.umbraco.org/wiki/how-tos/creating-reusable-content

  • 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