Could I pass some model into the template? #2
-
I cannot see in the examples if I can pass some Model to the template. It is possible ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Or how I could interfere with outside code ? |
Beta Was this translation helpful? Give feedback.
-
At present, I have designed two modes of operation depending on the first parsed token of the template:
In either mode, you have to provide two additional methods that must have the following signatures:
In fact, the second method can have overloads to avoid boxing. There is no requirement that the An example of 1 is the Transplator/eg/HelloWorld.cstt Lines 1 to 6 in ee1cb57 This will generate a partial class called In the sample project, the partial definition is then completed in Lines 30 to 40 in ee1cb57 It inherits from Lines 17 to 28 in ee1cb57 There is no requirement for inheritance. This is just one setup I came up with for the samples. The usage is then a simple call to the public and static Line 4 in ee1cb57 An example of mode 2 is Transplator/eg/Parameters.cstt Lines 1 to 24 in ee1cb57 As you can see, you have to provide the complete surrounding class and rendering method. You can name and define the class and method as you like. What's more, the rendering method can have whatever parameters you need. The model in the case above is a sequence of strings ( In the first mode of operation, you can also provide a model, but it would have to be done out-of-band with the
Here is how then you would adapt partial class HelloWorldTemplate : Template
{
HelloWorldTemplate(DateTime time, StringBuilder? sb = null) : base(sb) => Time = time;
DateTime Time { get; }
public static string Render(DateTime time)
{
var template = new ModelTemplate(time);
template.RenderCore();
return template.ToString();
}
} The public and static Hope this sheds some light into options for passing the model. |
Beta Was this translation helpful? Give feedback.
At present, I have designed two modes of operation depending on the first parsed token of the template:
Template
suffix. The class will have a single, private, instance-based and parameter-less method calledRenderCore
.{%
…%}
) then you are responsible for providing the entire class and method definition, thus having complete control over what and how you allow to be passed as a model (e.g. via one or more parameters).In either mode, you have to provide two additional methods that must have the following signatures:
WriteText(string value)
: called to e…