Copied to clipboard

Flag this post as spam?

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


  • Phil Young 19 posts 39 karma points
    Apr 27, 2012 @ 16:27
    Phil Young
    0

    Umbraco 5 and reusable content

    Hi all, I'm using a new installation of Umbraco 5 and trying to figure out whether I can make use of some reusable content, but using a macro to decide when its shown or not. I'll try to explain:

    I have a partial view (heroText.cshtml):

    <div id="hero-text">
      @Umbraco.Field("heroText")
    </div>

    This is made visible to the user as a property on the homepage template/doctype. Once the user enters their data, I want ot be able to use this on child pages without having to renter the value per page. I'd like them to enter the value once on the homepage, then have the contents inherited by the child pages.

    The child template I'd like to display this reusable content is called standard, which has the folloiwng macro partial:

    @Umbraco.RenderMacro("renderHeroText")

    The contents of this macro are:

    @if(DynamicModel.Children.Count() > 0) {
        <div id="hero-text">
            @Umbraco.Field("heroText")
        </div>
    }

    When I then view a child page that is using the standard template/doctype, the html is rendered, but NOT the contents that the editor supplied on the home page (where it does correctly render).

    Any ideas how I can achieve this?

    Thanks

  • Phil Young 19 posts 39 karma points
    Apr 27, 2012 @ 16:54
    Phil Young
    0

    This seems to work, but doesn't feel quite right:

    <div id="hero-text">

    @Html.Raw(DynamicModel.AncestorsOrSelf.Last().heroText)

    </div>

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Apr 27, 2012 @ 17:15
    Tom Fulton
    0

    Hi Phil,

    I think what you're asking for is to retrieve the value "recursively", which Umbraco.Field should have a built-in option for.  Try this:

    @Umbraco.Field("heroText", recursive: true)

    Note this will search all the way up the tree until it finds your property, so it's not necessarily always from the homepage - if you have "heroText" defined at a level between the current page and the homepage it will use the closest one.

    More info on Umbraco.Field options

    HTH,
    Tom 

  • Phil Young 19 posts 39 karma points
    Apr 27, 2012 @ 18:12
    Phil Young
    0

    Thanks Tom, that's great.

    Do you know how I can use a partial inside a macro:

    @Html.Partial("heroText")

    Otherwise I have to copy the HTML contants of that partial into the macro which is duplicating code.

     

    Thanks

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Apr 27, 2012 @ 18:15
    Tom Fulton
    0

    Not off the top of my head, but try this:

    @Html.Partial("MyPartial", Model.CurrentPage)

    (from http://issues.umbraco.org/issue/U5-261 )

    -Tom

  • Phil Young 19 posts 39 karma points
    Apr 30, 2012 @ 11:27
    Phil Young
    0

    Worked a treat, thanks Tom

Please Sign in or register to post replies

Write your reply to:

Draft