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.
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.
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 ?
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.
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
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>
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.
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
Thank you! It's very informative!
Thanks Alex, the GlobalHelpers solves it !
Cheers.
is working on a reply...