Repository navigation
Fluent API
Morestachio has a Fluent api that allows the manipulation of an Morestachio Document.
You can ether create a blank document and put that into the parser or use an existing template and parse that. When parsed the Parser returns an object of type MorestachioDocumentInfo. The MorestachioDocumentInfo has an method Fluent that allows access to the template.
Warning: the Fluent api modifies the Document directly, that means whatever you invoke through the FluentApi will be directly applied to the
MorestachioDocumentInfoit originates from.
Warning: The FluentAPI cannot extrapolate the character locations properly so do not try to compare a Document created from an string template with that created by the FluentAPI
Example Modifying Template:
//We want to create an template and replace the content inside the if
var documentInfo = await ParserOptionsBuilder.New()
.WithTemplate(@"{{test}}Hello {{#if test}}mr bombastic{{/if}}")
.BuildAndParseAsync();
var fluentApi = documentInfo.Fluent();
//We are searching for the if expression
fluentApi.FindNext<IfExpressionScopeDocumentItem>()
//the next contentDocumentItem is the content of the IF
.FindNext<ContentDocumentItem>()
//Remove that content from its parent. Remove will also go back to the last item in render order (s.b.)
.Remove()
//add your new content
.AddContent("World");The FluentAPI allows you to search ether in Render order (Bi-Directional) or in Hierarchical order (Tree). To Search the Render order you can use the SearchForward(FindNext) or SearchBackward(FindPrevious) methods. To search in Hierarchical order use the Parent and Child(N) methods. All searching methods will set the internal OperationStatus flag, you can use the If(Not)Success methods to react to that flag.
You can also alter the tree or its items with the FluentAPI.
Warning: Never change the
Childrenof anIDocumentItemdirectly but only with theAddChild(AndEnter)orRemoveas the internal tree of the FluentAPI must be generated accordingly
Morestachio includes several extension methods for creating the BuildIn Document items containing in MorestachioDocumentFluentApiExtensions. It also allows to use the ExpressionBuilder where you can create MorestachioExpression in a structured way.