Executing Functions

Execute CMS Functions in Razor page templates

There are two ways to execute CMS Functions in Razor page templates.

Using Function Markup

You can use the CMS Function's markup to execute it in a Razor page template (like with XSLT functions).

@inherits RazorPageTemplate
			  
@functions {
	public override void Configure()
	{
		TemplateId = new Guid("c6291e58-8589-49af-b573-272f108f9be3");
		TemplateTitle = "Template with Function Markup";
	}

	[Placeholder(Id="content", Title="Content", IsDefault=true)]
	public XhtmlDocument Content { get; set; }
}
<!DOCTYPE html>
<html lang="@Lang" xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>@CurrentPageNode.Title</title>
	</head>
	<body>
		<div>
			@Markup(Content)
		</div>
		<div>
			<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>
		</div>
	</body>
</html>

Download the sample

In the CMS Console, you can insert the function markup via the menu "Insert" | "Function Markup".

Using @Function helper method

The @Function helper method allows you to execute CMS Functions in a Razor page template. 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 RazorPageTemplate
			  
@functions {
	public override void Configure()
	{
		TemplateId = new Guid("3ef9ff47-213e-419c-8ea9-45e154817e1c");
		TemplateTitle = "Template with Function Helper Method";
	}

	[Placeholder(Id="content", Title="Content", IsDefault=true)]
	public XhtmlDocument Content { get; set; }
}
<!DOCTYPE html>
<html lang="@Lang" xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>@CurrentPageNode.Title</title>
	</head>
	<body>
		<div>
			@Markup(Content)
		</div>
		<div>
			@Function("Composite.Navigation.Distributed", 
				  new {Level=1, ShowParent="False", ShowChildPages="True", 
					   Expand="False", NavigationId="NavigationSideBar"})
		</div>
	</body>
</html>

Download the sample