Execute Other Functions

Execute other functions in Razor functions

There are two ways to execute other functions in your Razor functions.

Using Function Markup

By default Razor functions return an XhtmlDocument, so you can use the function's markup to execute the function (like with XSLT functions).

@inherits RazorFunction

@functions {
    public override string FunctionDescription
    {
        get  { return "A demo function that executes the Distributed Menu function."; }
    }
}

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://www.composite.net/ns/function/1.0">
    <head>
    </head>
    <body>
	<f:function name="Composite.Navigation.Distributed" xmlns:f="http://www.composite.net/ns/function/1.0" >
		<f:param name="Level" value="1" />
		<f:param name="ShowParent" value="False" />
		<f:param name="ShowChildPages" value="True" />
		<f:param name="Expand" value="False" />
		<f:param name="NavigationId" value="NavigationSideBar" />
	</f:function>			
    </body>
</html>

Download the sample

These functions will be executed when the markup returned by a Razor function is rendered by C1 CMS.

If you want the function executed before the Razor function returns, use the Function helper method.

Using @Function method

The @Function helper method allows you to execute other functions in a Razor functions before the Razor function returns. It has three overloads:

public IHtmlString Function(string name);
public IHtmlString Function(string name, IDictionary<string, object> parameters);
public IHtmlString Function(string name, object parameters);

The first parameter must be the name of the function to call (including the namespaces). The second parameter is only used to exectute functions with parametesr and is a list of parameters passed as either an object new {Param1=1, Param2=2}, or a Dictionary <string, string> {"Param1", 1}, {"Param1", 2}}.

@inherits RazorFunction

@functions {
    public override string FunctionDescription
    {
        get  { return "A demo function that executes the Distributed Menu function."; }
    }
}

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://www.composite.net/ns/function/1.0">
    <head>
    </head>
    <body>
		@Function("Composite.Navigation.Distributed", 
				  new {Level=1, ShowParent="False", ShowChildPages="True", 
					   Expand="False", NavigationId="NavigationSideBar"})
    </body>
</html>

Download the sample