x First time here? Check out the FAQ

Come work for Umbraco - The Umbraco HQ are hiring Project managers, .NET developers and DevOps people!

  • Avatar11posts32karma

    Including and sharing variable throug Razor files

    Georges started this topic May 25, 2011 @ 07:38, Go directly to the topic solution

    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.


  • Avatar195posts392karma
    Comment with ID: 78288
    Dimitri Kourkoulis posted this reply May 26, 2011 @ 07:47

    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 posted this reply May 26, 2011 @ 02:09

    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>

     


  • Avatar11posts32karma
    Comment with ID: 78393
    Georges posted this reply May 26, 2011 @ 07:53

    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.


  • Avatar78posts135karma
    Comment with ID: 78487
    Alex posted this reply May 27, 2011 @ 01:56

    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


  • Avatar195posts392karma
    Comment with ID: 78489
    Dimitri Kourkoulis posted this reply May 27, 2011 @ 03:22

    Thank you! It's very informative!


  • Avatar11posts32karma
    Comment with ID: 78492
    Georges posted this reply May 27, 2011 @ 03:44

    Thanks Alex, the GlobalHelpers solves it !

    Cheers.


Please login or Sign up To post replies