RazorComponent aka Razor Templates
Razor Components contain both normal HTML intermixed with Razor placeholders like
@Content.FirstName or longer code blocks usually marked with
All razor Templates derive from the RazorComponent, so the following variables and objects are available for you to work with.
To use the latest features of 2sxc 10.20+, make sure your razor file begins with the line
Otherwise your Razor templates is based on the default, older component code.
Visit the App Catalog where almost all apps use Razor. There you can find hundreds of examples.
Objects Added by 2sxc in Razor Components / Templates
- App - the current App and all it's data
- Content (DynamicEntity) - primary and often the only content-item in the Data for this template
- Content.Presentation (DynamicEntity)
- Data (IDataSource)- this object gives you all the data which was meant to be used by this Templates
- Dnn - the common Dnn object providing page, module, user information
- Edit - helper providing you with various edit-functionality like
- Header (DynamicEntity) - the header data if the template expects to be a list
- Header.Presentation (DynamicEntity)
- Link - helper to generate links, according to the DNN-environment configuration
Helper Commands provided by 2sxc
- AsDynamic(...) - takes just about anything (an iEntity, a list of iEntities, a dynamic, ...) and casts it to a DynamicEntity
- AsEntity(...) - takes just about anything (iEntity, DynamicEntity, list of that) and casts it to an iEntity
- CreateInstance(...) - to create an object of a parsed CSHTML file, for example to then access methods of that code
- CreateSource<T>(...) (IDataSource) - more modern, generic, type-proof syntax for create-source
Customizing Data & Search
OverrideableCustomizeData - is like a "before-data-is-used" of the page, used to change what data is delivered to the page - or to the search.
Note that this is an older feature and many things this does can also be done using the visual query designer. But sometimes you will need code, and this is the place to do it.
stringPurpose - tells you if the code is running to render into html, or for another reason like populating the search index - so your code can adapt
Migrating from the old Razor (before 10.20) to the new RazorComponent
The RazorComponent was created in v10.20 to provide a newer, cleaner API. To not break existing code, old templates still work, but you must migrate a template to RazorComponent if you wish to use the new features. Here what you need to do: