Skip to main content
Logo

Develop with Razor views


Developing Razor views within Contensis is really easy, all you need to do is create a new Razor view, add it to a template or page and you are ready to start coding.

The Razor view model

As part of a Contensis Razor view there are a number of properties available to you directly within the view context:

  • CurrentNode - This is the ContentNode instance that represents the data for the current page.
  • CurrentContext - This AppContext singleton instance represents the context of the current page and the website state.It includes convenient access to properties that you will want to use all the time such as the page object, project settings, the taxonomy tree, localisation values and security objects.
  • CurrentUser - A convenient shortcut to the User instance, which can also be accessed through the Security property on the CurrentContext.

CurrentNode

The CurrentNode property is where you go to get the data for the current page. The example below shows how to simply output the title of the current page in a Heading tag.

<h1>@CurrentNode.Title</h1>

Once you have the current node you can then navigate to other nodes such as siblings or children to access their properties. The example below demonstrates how to list the content of a sub folder of the current node.

@using Contensis.Framework.Web
 
@{
    // The parent will always be a FolderNode.
    var parent = CurrentNode.Parent;
 
    // Get all the child folder nodes and then use LINQ to select the folder by title.
    var subFolder = parent.Children<FolderNode>().Where(f => f.Title == "SubFolder").First();
}
 
<ul>
 
@foreach (var page in subFolder.Children<ContentNode>())
{
    <li><a href="@page.Path">@page.Title</a></li>
}
</ul>

CurrentContext

The CurrentContext property is used when you need data other than that of the CurrentNode. 

Localisation

The example below demonstrates how to get the localised value of a label which has an id of 12345 using the Get() method on the Localisation property.

// The localisation language being specified is the language set for the page.
<div>@CurrentContext.Localisations.Get(12345, CurrentNode.Language)</div>

Taxonomy

The example below demonstrates how to get a taxonomy node by path and then list the child keys out into a combobox.

@using Contensis.Framework.Web;
 
@{
    // This could use the GetByKey() method instead.
    var node = CurrentContext.Taxonomy.GetByPath("StructuredContent/Events/Categories/", TaxonomySortOrder.Alphabetical);
}
 
<select>
@foreach (var childNode in node.Children)
{
    <option value="@childNode.Key">@childNode.Value</option>
}
</select>

Security

The example below shows how find a group by name and then access all it's members

// Get all users of the Developers group
var users = CurrentContext.Security.Groups.FindBy("Developers").Members;

CurrentUser

The example below shows how to dynamically display markup dependant on whether the current user is logged in.

// Only display the markup if the current user is logged in.
@if(CurrentUser.IsAuthenticated)
{
    <h2>Welcome back @CurrentUser.DisplayName</h2>
}