Copied to clipboard

Flag this post as spam?

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


  • Pavel Gurecki 55 posts 158 karma points
    Apr 04, 2013 @ 14:28
    Pavel Gurecki
    1

    Razor extension - How to in Umbraco 6 MVC (with helpers)

    Hello,

    I was browsing for ways to implement custom extensions for razor or simply adding my custom global functions. Found posts are quite outdated. Best one is: http://our.umbraco.org/forum/developers/razor/21378-Razor-extension-methods but its solution is not working for me.

    What I learnt so far about ways to implement custom extensions:

    1. Create razor scripts in App_Code folder (using @helper or @functions annotations)
    2. Create class in App_Code folder (using umbraco.MacroEngines; as in previous mentioned post)
    3. Create class library(same as 2) and drop .dll into bin folder 
    1 seems to be the best solution, the biggest problem is that I don't know how to access umbraco helpers. I pass it as parameter:
    App_Code/GlobalFunctions.cshtml
    @using Umbraco
    @using Umbraco.Core.Models
    @using Umbraco.Web
    @functions {
        public static String GetThumbnailUrl(IPublishedContent post, UmbracoHelper umbracoHelper, String cropName = "")
        {
    MacroPartial
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    ....
    String thumbnailUrl = GlobalFunctions.GetThumbnailUrl(author, Umbraco, "blog-author-thumb");
    I think there is better solution to get helpers in App_Code/GlobalFunctions.cshtml

    So the questions are*: 
    • What is the best option to write extension methods for Razor?
    • How to access helpers in extension code?

    *Umbraco 6.0.3 MVC

     

  • Morten Jensen 41 posts 104 karma points
    Jun 12, 2013 @ 09:33
    Morten Jensen
    0

    I have the same challenge, so i am listening. 

  • Jonas Eriksson 930 posts 1825 karma points
    Jun 17, 2013 @ 16:22
    Jonas Eriksson
    103

    Hi,

    You can add it as an extension method on UmbracoHelper:

    using Umbraco.Web;
    namespace MyUmbracoHelperExtensions
    {
        public static class UmbracoHelperExtensions
        {
            public static string MyCropThing(this UmbracoHelper umbraco, string cropName)
            {
                // has access to umbracohelper methods...
    return "something"; } } }

    (Save it in App_Code or compile it to a dll). Then use it like so:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using MyUmbracoHelperExtensions
    @Umbraco.MyCropThing("foo")
    
    That said - you should be able to use App_Code static helpers, and pass the umbracohelper as an argument, as you did, do you get errors? I tried in Umbraoc 6.1.0 and it worked fine:
    App_Code/MyHelper.cshtml
    @helper HelperHelloHelper(string name, IPublishedContent content, UmbracoHelper umbracoHelper) {
        <h1>Hello @name</h1>     
        @content.Id
        @umbracoHelper.Coalesce("1","2")
    }
    @functions{
    public static string FunctionsHelper(UmbracoHelper umbracoHelper) {
    return "";
    }
    MyView.cshtml
    @MyGlobalHelper.HelperHelloHelper("foo",Model.Content, Umbraco)
    @MyGlobalHelper.FunctionsHelper(Umbraco) 
  • Pavel Gurecki 55 posts 158 karma points
    Nov 24, 2013 @ 16:41
    Pavel Gurecki
    3

    Quite outdated post but I'll share my experience on this, maybe it will be helpful for somoene.

    I think the best option is to use Umbraco helper extensions as Jonas suggested and put it to separate project/dll. Main advantage of putting helpers to separate dll is that they can be easily reused in other projects too (I've had custom sorting function which I wanted to use in WebApi project but couldn't do that when function was placed in App_Code of website project).

    So what I've done is created separate project (Extensions.Helpers) with classes to categorize helpers (ImageHelpers, UmbracoNodeHelpers, GlobalHelpers, etc.) as extension methods for UmbracoHelper, IPublishedContent, HtmlHelper and any other that seemed correct. Then I would need to import my helpers namespace in templates and use them straight away (as Jonas showed in previous post ). Although I still use App_Code for helpers that are website specific and won't be used in any other projects, as they don't require any namespace to be imported.

    P.S. one of my initial questions was "How to access helpers in extension code?". Ofcourse you could just pass it like a parameter to helper function, but I was looking for a way without doing that.

    When exploring umbraco wiki I found this code:

    if (UmbracoContext.Current != null) 
    {
    var umbHelper = new UmbracoHelper(UmbracoContext.Current);
    }

    That's exactly what I was looking for, as you can get UmbracoHelper from UmbracoContext, which is accessible from default Umbraco namespaces, so you won't need to pass it as a parameter.

    So with everything noted here, my helper code went from:

    String thumbnailUrl = GlobalFunctions.GetThumbnailUrl(author, Umbraco, "blog-author-thumb");

    to:

    @import Extensions.Helpers
    ...
    String thumbnailUrl = author.GetThumbnailUrl("blog-author-thumb");


    That's only a small detail, but code got a bit more readable now. Next step could be setting up auto import of "Extensions.Helpers" namespace to all templates.

  • Max 14 posts 44 karma points
    Apr 17, 2014 @ 08:17
    Max
    0

    Hi Pavel!

    Thanks for your experience!
    I was looking exactly for this solution to get rid from many "Umbraco" parameters for GlobalHelper, passed from Views :)

Please Sign in or register to post replies

Write your reply to:

Draft