StubbleOrg / Stubble

Trimmed down {{mustache}} templates in .NET
Other
399 stars 58 forks source link

Get output as object rather than string from Render #117

Closed Alik2015 closed 2 years ago

Alik2015 commented 2 years ago

As Render(template,obj) returns a string. Say you were building an Email Templating system and wanted to fill in subject and body of the email where subject is not part of the actual body would you need to call Render twice as follows:


string template = "{{Name}} other email body stuff";
string body = Render(template,obj);
string Subject = Render("Reference Number: {{Ref}}",obj);

public class MyObj
{
public string Ref{get;set;}
public string Name{get;set;}
}
`

Would calling Render twice not have performance issues?``
Romanx commented 2 years ago

Hey there,

The problem here is that you're templating two seperate templates. One being the body of your email and the second being the subject. If you're reusing these often then you can use our compilation feature to create a function from your template so we don't keep recompiling.

In the past when i've done a similar task the subject tends to be quite static and can usually be generated using a string interpolation.

The body tends to be more compilated and so templating that using stubble would be reasonable since you may have optional sections or loops.

Hopefully this helps some.

Alik2015 commented 2 years ago

It helps and I was just using subject as an example but would be good if you had a simple example of the function you mentioned. On 8 Sep 2021 09:55, Alex McAuliffe @.***> wrote: Hey there, The problem here is that you're templating two seperate templates. One being the body of your email and the second being the subject. If you're reusing these often then you can use our compilation feature to create a function from your template so we don't keep recompiling. In the past when i've done a similar task the subject tends to be quite static and can usually be generated using a string interpolation. The body tends to be more compilated and so templating that using stubble would be reasonable since you may have optional sections or loops. Hopefully this helps some.

—You are receiving this because you authored the thread.Reply to this email directly, view it on GitHub, or unsubscribe.

Romanx commented 2 years ago

The notes on compilation are here. Without knowing your requirements i can't really offer more.

Alik2015 commented 2 years ago

Great appreciate that that is how I was doing it anyway.On 8 Sep 2021 10:21, Alex McAuliffe @.***> wrote: The notes on compilation are here. Without knowing your requirements i can't really offer more.

—You are receiving this because you authored the thread.Reply to this email directly, view it on GitHub, or unsubscribe.