Copied to clipboard

Flag this post as spam?

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


  • Vakman 5 posts 26 karma points
    Oct 21, 2011 @ 00:16
    Vakman
    1

    Library class not available in global helper class

    I'm trying to write a razor helper accessible to all of my razor templates.  The cshtml file for the helper has been placed in the Umbraco website App_Code directory.  I'm trying to use Library.MediaById to pull back the media object for a specific media id.  However I'm getting an exception: The name 'Library' does not exist in the current context.

    The razor template below:

    @using umbraco.MacroEngines
    @using umbraco.MacroEngines.Library
    @helper Image(dynamic mediaId, dynamic altText)
    {
        if (mediaId != null && (mediaId.GetType() != typeof(string) || mediaId != string.Empty))
        {
            var media = Library.MediaById(mediaId);
            <img src="@media.umbracoFile" alt="@altText"  />
        }
    }

    Any ideas?

  • Rodion Novoselov 694 posts 859 karma points
    Oct 21, 2011 @ 00:46
    Rodion Novoselov
    0

    Hi. 'Library' is actually an instance property, whereas a helper is internally compiled to a static method. You cannot call any instance member from a static member.

  • Vakman 5 posts 26 karma points
    Oct 21, 2011 @ 03:42
    Vakman
    0

    Right. is there a static method that can be used to load a media item given an id?

  • Rodion Novoselov 694 posts 859 karma points
    Oct 21, 2011 @ 08:10
    Rodion Novoselov
    3

    It seems that

    var media = new RazorLibraryCore(null).MediaById(mediaId);

    should work well.

  • Funka! 398 posts 661 karma points
    May 11, 2012 @ 04:13
    Funka!
    0

    Thank you, Rodion, this does certainly work well! (And saved my bacon!)

    However, it seems kind of like .... odd usage ... and worries me this may break unexpectedly at some point in the future?  Even looking into the source code, as I find myself doing more and more of these days, it doesn't seem too clear that passing null to the RazorLibraryCore constructor is OK or not.

    If so, and if this really is the recommended approach, it seems maybe cleaner if the Umbraco team would overload this constructor to not require any constructor at all, or create a static node loader we could use instead... just to show that this is OK and supported thing to do??

    Thank you!

  • Funka! 398 posts 661 karma points
    May 11, 2012 @ 04:23
    Funka!
    0

    It occurs to me now that I suppose I could always pass Library into my helpers as one of the parameters? This would mean my (static, global) helper method would have access to a "properly-instantiated" RazorLibraryCore object instead of the "null-instantiated" one... I would just need to adjust the signature and all usage to do this I guess.

    So fitting in with the OP example, instead of:

    @helperImage(dynamic mediaId, dynamic altText)

    We would see this:

    @helperImage(dynamic mediaId, dynamic altText, RazorLibraryCore library)

    And then BAM!  Pass it in each time and we have access to good ol' @Library in its intended state.  Yet another trick to throw into the bag!

    Best Regards!

Please Sign in or register to post replies

Write your reply to:

Draft