Copied to clipboard

Flag this post as spam?

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


  • Georges 11 posts 32 karma points
    May 25, 2011 @ 19:38
    Georges
    0

    Including and sharing variable throug Razor files

    Hi All, 

    In order to include and share an XSLT file we use <xsl:include>. What is the equivalent in Razor synthx ? We need to declare some global parameter wthin a signle razor File, and have access to thaese variables throughout all the razor files. 

    Any way around ?

    Cheers.

    Georges.

  • dimi309 245 posts 579 karma points
    May 26, 2011 @ 07:47
    dimi309
    0

    This is a very good question!

    I know that you can use RenderPage("SomeOtherScript.cshtml") in a razor script to render the output of another script. It is quite useful because you can even use it in code like

    string a = RenderPage("SomeOtherScript.cshtml")

    However it is only the output that is reused this way. If anyone knows if one can actually reuse variables and methods in a script from another script, please let us know.

     

    Dimitri

  • Alex 78 posts 136 karma points
    May 26, 2011 @ 14:09
    Alex
    0

    There may be a better way but you could always use the Session object. Or the pages Items dictionary, like this:

    <umbraco:Macro runat="server" language="cshtml">
      @{
      Page page = HttpContext.Current.Handler as Page;
      page.Items.Add("MyVar", "Hello World");
      }
    </umbraco:Macro>

    <umbraco:Macro runat="server" language="cshtml">
      @{
      Page page = HttpContext.Current.Handler as Page;
      @page.Items["MyVar"];
      }
    </umbraco:Macro>

     

  • Georges 11 posts 32 karma points
    May 26, 2011 @ 19:53
    Georges
    0

    Thanks Alex. We were thinking of using cookies, your answer goes in the same direction.

    As of declaring some commun inline functions that can be shared acress the multiple cshtml, do you have any solutions other that putting these functions in a custom DLL ? 

    Cheers.

  • Alex 78 posts 136 karma points
    May 27, 2011 @ 13:56
    Alex
    1

    Hi,

    Check out this blog post http://joeriks.wordpress.com/2011/03/11/better-structure-for-your-razor-scripts-with-renderpage-in-umbraco/ especially the bit about GlobalHelpers at the bottom.

    It also has a bit about utilising the @Page object but I couldn't get that to work inside Macros.

    Hope that helps

    Alex

  • dimi309 245 posts 579 karma points
    May 27, 2011 @ 15:22
    dimi309
    0

    Thank you! It's very informative!

  • Georges 11 posts 32 karma points
    May 27, 2011 @ 15:44
    Georges
    0

    Thanks Alex, the GlobalHelpers solves it !

    Cheers.

Please Sign in or register to post replies

Write your reply to:

Draft