Copied to clipboard

Flag this post as spam?

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


  • Tim C 161 posts 528 karma points
    Jun 29, 2016 @ 07:50
    Tim C
    0

    I am probably missing something obvious, but is there any way of having some common Razor that I can share between templates.

    For example, every page has properties pageTitle and bannerImage : I would like some common razor that I can have on each template without having to paste it into each one eg

    @{
        var pageTitle = Umbraco.Field("PageTitle");
        var bannerImageId = Int32.Parse("0" + Model.Content.GetPropertyValue("bannerImage").ToString());
        var bannerImageUrl = "";
        if(bannerImageId != 0){
          bannerImageUrl = Umbraco.Media(bannerImageId).Url;
    }
    

    I can't put it into the parent template as variables don't seem to be inherited by child templates(?) nor from partial views.

    Is there any way of sharing Razor snippets to allow central storing of common razor?

  • David Peck 687 posts 1863 karma points c-trib
    Jun 29, 2016 @ 08:17
    David Peck
    2

    This is just C# - it doesn't render anything. I would create an extension method of IPublishedContent. Then you could do something like:

    @ModelContent.GetBannerImageUrl()
    

    I've seen something about creating functions in the App_Code folder. So you could check that out, but it's not something I've done myself.

  • Rhys Hamilton 20 posts 89 karma points
    Jun 29, 2016 @ 14:51
    Rhys Hamilton
    1

    If every page has these properties (as you mentioned) then perhaps a simple solution would be to set them within the _Layout.cshtml?

  • David Peck 687 posts 1863 karma points c-trib
    Jun 29, 2016 @ 15:31
    David Peck
    0

    I'm pretty sure that your page views or partials can access the layout, but by all means try it. Let me know if you can!

  • David Peck 687 posts 1863 karma points c-trib
    Jul 01, 2016 @ 08:11
    David Peck
    0

    That should have said 'can't access'.

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Jun 30, 2016 @ 12:55
    Paul Seal
    0

    I would have the common razor in a partial and call it from the template like this

    @{ Html.RenderPartial("_Header"); }

  • David Peck 687 posts 1863 karma points c-trib
    Jul 01, 2016 @ 08:12
    David Peck
    0

    If the common razor is rendering HTML and not computational then I absolutely agree.

  • Tim C 161 posts 528 karma points
    Jul 01, 2016 @ 09:16
    Tim C
    0

    I am trying to use it to set c# variables, so I don't believe a partial will do as variables are local to the partial, I think.

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Jul 01, 2016 @ 10:02
    Paul Seal
    0

    Then I would get the partial code to get the values and store them in the cache or session. Then it can check for them first and get them if they have not been set.

  • David Peck 687 posts 1863 karma points c-trib
    Jul 01, 2016 @ 10:44
    David Peck
    0

    Yes, adding variables to the request cache would work, but I still think an extension method is the way to go.

Please Sign in or register to post replies

Write your reply to:

Draft