-
Notifications
You must be signed in to change notification settings - Fork 11
Quickstart Developer
There are two essentaial features you can include in your app. The Document engine Morestachio and its Expression parser.
If you want to put Data and Text together you must not prepare them in any special way. You can put anything into Morestachio that has properties, you could even just put an EntityFramework DataContext into your template without any problems but if you want to use Formatters in your code you have to add them to Morestachio.
Start with an ParserOptions class. You have to supply at least the template you want to parse:
Plain Template options
var template = "hello mr {{Data.Name}}";
var options = new Morestachio.ParserOptions(template);
As morestachio is based on Streams for writing the result "anywhere" you might also want to provide a function to supply a stream:
FileStream Template options
var template = "hello mr {{Data.Name}}";
var options = new Morestachio.ParserOptions(template, () => new FileStream(@"\tempFiles\generatedTemplate.txt");
There are plenty of other options you can use with the ParserOptions object like what Encoding should be used or how Null values should be rendered and you can also set a Timeout for the rendering process.
The next step is to put the ParserOptions object into the Parser. The Parser will create the document tree based on the template within the ParserOptions object.
Parse Template options
var template = "hello mr {{Data.Name}}";
var options = new Morestachio.ParserOptions(template);
MorestachioDocumentInfo document = Morestachio.Parser.ParseWithOptions(options);
The MorestachioDocumentInfo contains the Parsed template in form of the DocumentTree that starts with the MorestachioDocumentInfo.Document property. You can now call ether CreateAsync or CreateAsyncAndStringify to create the template. The MorestachioDocumentInfo instance can be held in memory as long as needed, can be reused and also executed in parallel.
To create the template you have to supply the Data you want to insert into the Template. This data can be anything, but there is build in support for IDictionary<string, object> and any dictionaries of that kind are handled as full fledged objects.
var template = "hello mr {{Data.Name}}";
var options = new Morestachio.ParserOptions(template);
MorestachioDocumentInfo document = Morestachio.Parser.ParseWithOptions(options);
var data = new Dictionary<string, object>()
{
{
"Data", new Dictionary<string, object>()
{
{"Name", "Johnson"}
}
}
};
//this is the same as
var data = new
{
Data = new
{
Name = "Johnson"
}
};
Stream result = await document.CreateAsync(data);
The return of Create async is now your created template.