Templates
Templates provide a simple way of content generation using well-known Razor language syntax (see cheat-sheet below). They are very useful for generating content like email notifications, documents, contracts, etc.
Creating a template
Template code language
This setting is primarily about developer's comfort during preparation of a template.
Language | Syntax highlighting | Preview | Usage |
---|---|---|---|
None | No | No | Arbitrary strings you need to generate. |
Html | HTML + Razor | Yes | Html pages, snippets. Useful for emails. |
Markdown | MD + Razor | Yes | Markdown texts, usually converted into HTML later. |
Model type
Model type defines input which the template receives. It's available as Model variable inside the model.
Type | C# type of Model | Usage |
---|---|---|
Object | Object | Arbitrary models, often used for custom classes or for templates without a model |
String | String | Useful for nesting templates |
String Enumerable | IEnumerable<string> | Useful for nesting templates |
Entity | EntityType | Templates for entity |
Entity Enumerable | IEnumerable<EntityType> | Templates for entity collection |
Aspect | AspectType | Templates for entity inheriting an aspect |
Aspect Enumerable | IEnumerable<AspectType> | Templates for collection of entities which inherit an aspect |
Using the Object Model
The object model allows you to pass any arguments you need. To do this, create a class or record with the desired attributes, and then cast the model as the class or record instance in the template.
Ensure the attributes are correct, as this is not checked at compile time.
Usage
Every template you define is accessible through App.Templates object. Each template has a Render(model) method which requires different input based on selected Model Type.
MyEntity entity = db.MyEntitySet.First();
string rendered = App.Templates.MyTemplate.Render(entity);