This demonstrates how to use the new TemplateService on Kit.Template.
Kit.Template
Parse(...)
It's already [DateTime:Now]
It's already [DateTime:Now|yyyy-MM-dd HH:mm:ss]
The current tutorial is [QueryString:tut]
The current URL is [Request:Url]
@inherits Custom.Hybrid.RazorTyped <h4>Use Quick and simple <code>Parse(...)</code></h4> @{ var dateTemplate = "It's already [DateTime:Now]"; var dateFormatted = "It's already [DateTime:Now|yyyy-MM-dd HH:mm:ss]"; var urlParam = "The current tutorial is [QueryString:tut]"; var urlTemplate = "The current URL is [Request:Url]"; } <ul> <li> <strong>Template:</strong> <code>@dateTemplate</code> <br> <strong>Result:</strong> @Kit.Template.Parse(dateTemplate) </li> <li> <strong>Template:</strong> <code>@dateFormatted</code> <br> <strong>Result:</strong> @Kit.Template.Parse(dateFormatted) <li> <strong>Template:</strong> <code>@urlParam</code> <br> <strong>Result:</strong> @Kit.Template.Parse(urlParam) </li> <li> <strong>Template:</strong> <code>@urlTemplate</code> <br> <strong>Result:</strong> @Kit.Template.Parse(urlTemplate) </li> </ul>
I just wanted to say [Hello:World]
[Hello:John] [Bye:Samantha]
@inherits Custom.Hybrid.RazorTyped <h4>Use a Function based Source</h4> @{ var fnTemplate = "I just wanted to say [Hello:World]"; var source1 = Kit.Template.CreateSource("Hello", key => $"Hello {key} from a function"); var engine1 = Kit.Template.Empty(sources: new [] { source1 }); var helloBye = "[Hello:John] [Bye:Samantha]"; var source2 = Kit.Template.CreateSource("Bye", key => $"Goodbye {key} from a function"); var engine2 = Kit.Template.Empty(sources: new [] { source1, source2 }); } <ul> <li> <strong>Template:</strong> <code>@fnTemplate</code> <br> <strong>Result:</strong> @engine1.Parse(fnTemplate) </li> <li> <strong>Template:</strong> <code>@helloBye</code> <br> <strong>Result:</strong> @engine2.Parse(helloBye) </li> </ul>
<strong>[Values:Greeting]</strong> <br> <em>[Values:Intro]</em>
<strong>Hello there 👋🏾</strong> <br> <em>This is a simple dic-sample</em>
@inherits Custom.Hybrid.RazorTyped <h4>Use a Dictionary based Source</h4> @{ var template = "<strong>[Values:Greeting]</strong> <br> <em>[Values:Intro]</em>"; var dic = new Dictionary<string, string> { { "Greeting", "Hello there 👋🏾" }, { "Intro", "This is a simple dic-sample" } }; var dicSource = Kit.Template.CreateSource("Values", dic); var engine = Kit.Template.Empty(sources: new [] { dicSource }); } <ul> <li> <strong>Template:</strong> <code>@template</code> </li> <li> <strong>Result:</strong> <br> <code> @engine.Parse(template) </code> </li> <li> <strong>Result Raw:</strong> <br> @Html.Raw(engine.Parse(template)) </li> </ul>
<strong>[Person:FirstName] [Person:LastName]</strong> <br> <em>Fav. Number: [Person:FavoriteNumber]</em>
<strong>Douglas Adams</strong> <br> <em>Fav. Number: 41.99</em>
@inherits Custom.Hybrid.RazorTyped @using AppCode.Data <h4>Use a Entity/Item based Source</h4> @{ var template = @"<strong>[Person:FirstName] [Person:LastName]</strong> <br> <em>Fav. Number: [Person:FavoriteNumber]</em>"; var item = App.Data.GetAll<Persons>().First(); var dicSource = Kit.Template.CreateSource("Person", item); var engine = Kit.Template.Empty(sources: new [] { dicSource }); } <ul> <li> <strong>Template:</strong> <code>@template</code> </li> <li> <strong>Result:</strong> <br> <code> @engine.Parse(template) </code> </li> <li> <strong>Result Raw:</strong> <br> @Html.Raw(engine.Parse(template)) </li> </ul>
<p><strong>[Person:FirstName] [Person:LastName]</strong><br><em>[Person:Introduction||...is a great author!]</em></p>
<p><strong>Douglas Adams</strong><br><em>...is a great author!</em></p>
@inherits Custom.Hybrid.RazorTyped @using AppCode.Data <h4>Use Item-Source with Template in another Item</h4> @{ // Get the template item var templateItem = App.Data.GetAll<ServiceTemplateHtml>().First(); // Get the item to use as source var item = App.Data.GetAll<Persons>().First(); var source = Kit.Template.CreateSource("Person", item); var engine = Kit.Template.Empty(sources: new [] { source }); } <ul> <li> <strong>Template:</strong> <code>@templateItem.Template</code> </li> <li> <strong>Result:</strong> <br> <code> @engine.Parse(templateItem.Template) </code> </li> <li> <strong>Result Raw HTML using Html(..., tweak: ...)</strong> <br> @templateItem.Html("Template", tweak: t => t.Input(engine.Parse)) </li> </ul>
@inherits Custom.Hybrid.RazorTyped @using AppCode.Data <h4>Use Item-Source with Template in another Item</h4> @{ // Get the template item var templateItem = App.Data.GetAll<ServiceTemplateHtml>().First(); } <ul> @foreach(var item in App.Data.GetAll<Persons>()) { // Create a source for this loop iteration var source = Kit.Template.CreateSource("Person", item); // Get the default engine with this source var engine = Kit.Template.Default(sources: new [] { source }); <li> @* Call the engine, and give it a special source just for this loop *@ @templateItem.Html("Template", tweak: t => t.Input(engine.Parse) ) </li> } </ul>
@inherits Custom.Hybrid.RazorTyped @using AppCode.Data <h4>Use Merge to combine 2 sources</h4> @{ // Get the template item var templateItem = App.Data.GetAll<ServiceTemplateHtml>().First(); } <ul> @foreach(var item in App.Data.GetAll<Persons>()) { // Create a source for this loop iteration var itemSource = Kit.Template.CreateSource("Main", item); // Create an override-source to override certain values var isFavorite = item.FirstName == "Douglas" || item.FirstName == "Terry"; var overrideSource = Kit.Template.CreateSource("Override", new Dictionary<string, string> { { "FirstName", item.FirstName + (isFavorite ? " 💫" : "") }, { "Introduction", isFavorite ? "is an awesome 🦾 author!" : null } }); // Merge the sources so the custom one takes precedence var mergedSource = Kit.Template.MergeSources("Person", new [] { overrideSource, itemSource }); // Get the default engine with this source var engine = Kit.Template.Default(sources: new [] { mergedSource }); <li> @* Call the engine, and give it a special source just for this loop *@ @templateItem.Html("Template", tweak: t => t.Input(engine.Parse)) </li> } </ul>