Troubleshooting: No Output
If you’re not getting any output from your template, look at these issues first.
1. Your Member Access is Too Restrictive
By default, no classes are authorized to allow output. When this happens, they don’t throw an error, they just go silent.
You can negate this entire principle if you like:
TemplateOptions.Default.MemberAccessStrategy = new UnsafeMemberAccessStrategy();
That will certainly fix it, though there are other, more granular methods. See Member Access and Controlling Template Data.
2. Your Templates are Case-Sensitive
By default, templates are case-sensitive, so {{ x.title }}
is not going to produce anything from the Title
property of your object. Again, this will be silent.
This is also modified through member access.
TemplateOptions.Default.MemberAccessStrategy = new UnsafeMemberAccessStrategy()
{
IgnoreCasing = true
};
3. You Didn’t Push Anything Into the Context
Nothing is available to your template automatically.
You have to explicitly push data into your context, then pass that context into the Render
method.
var context = new TemplateContext();
context.SetValue("name", "Deane");
var result = template.Render(context);
See Passing Data into Templates for more on this.
4. You Mistakenly Think There’s a “Model” Variable
If you pushing a model into the context, you might think there’s an actual variable called Model
. There is not. (If you’re coming from Razor, this is different…)
Properties on the model are “top-level.”
var context = new TemplateContext(new { name = "Deane" });
{{ model.name }} will output nothing.
There's no explicit "model" object.
{{ name }} is what you want.
A model property is assumed if there's no other value in the scope
Again, see Passing Data into Templates for much more on this.