Skip to main content
Home  › ... Razor

Template Service

Tutorial HomeTemplate-Service

Basic Examples with the Template Service.

This demonstrates how to use the new TemplateService on Kit.Template.

⬇️ Result | Source ➡️

Use Quick and simple Parse(...)

  • Template: It's already [DateTime:Now]
    Result: It's already 11/2/2024 7:39 PM
  • Template: It's already [DateTime:Now|yyyy-MM-dd HH:mm:ss]
    Result: It's already 2024-11-02 19:39:04
  • Template: The current tutorial is [QueryString:tut]
    Result: The current tutorial is templateservice
  • Template: The current URL is [Request:Url]
    Result: The current URL is
@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>

⬇️ Result | Source ➡️

Use a Function based Source

  • Template: I just wanted to say [Hello:World]
    Result: I just wanted to say Hello World from a function
  • Template: [Hello:John] [Bye:Samantha]
    Result: Hello John from a function Goodbye Samantha from a function
@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>

⬇️ Result | Source ➡️

Use a Dictionary based Source

  • Template: <strong>[Values:Greeting]</strong> <br> <em>[Values:Intro]</em>
  • Result:
    &lt;strong&gt;Hello there &#128075;&#127998;&lt;/strong&gt; &lt;br&gt; &lt;em&gt;This is a simple dic-sample&lt;/em&gt;
  • Result Raw:
    <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>

⬇️ Result | Source ➡️

Use a Entity/Item based Source

  • Template: <strong>[Person:FirstName] [Person:LastName]</strong> <br> <em>Fav. Number: [Person:FavoriteNumber]</em>
  • Result:
    &lt;strong&gt;Douglas Adams&lt;/strong&gt; &lt;br&gt; &lt;em&gt;Fav. Number: 41.99&lt;/em&gt;
  • Result Raw:
    <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>

⬇️ Result | Source ➡️

Use Item-Source with Template in another Item

  • Template: <p><strong>[Person:FirstName] [Person:LastName]</strong><br><em>[Person:Introduction||...is a great author!]</em></p>
  • Result:
    &lt;p&gt;&lt;strong&gt;Douglas Adams&lt;/strong&gt;&lt;br&gt;&lt;em&gt;...is a great author!&lt;/em&gt;&lt;/p&gt;
  • Result Raw HTML using Html(..., tweak: ...)
    <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>

⬇️ Result | Source ➡️

Use Item-Source with Template in another Item

  • <p><strong>Douglas Adams</strong><br><em>...is a great author!</em></p>
  • <p><strong>Terry Pratchett</strong><br><em>...is a great author!</em></p>
  • <p><strong>Neil Gaiman</strong><br><em>...is a great author!</em></p>
  • <p><strong>George Akerlof</strong><br><em>...is a great author!</em></p>
  • <p><strong>Raphael Müller (not an author)</strong><br><em>...is a great author!</em></p>
  • <p><strong>Ed Hardy</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();
}
<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>

⬇️ Result | Source ➡️

Use Merge to combine 2 sources

  • <p><strong>Douglas 💫 Adams</strong><br><em>is an awesome 🦾 author!</em></p>
  • <p><strong>Terry 💫 Pratchett</strong><br><em>is an awesome 🦾 author!</em></p>
  • <p><strong>Neil Gaiman</strong><br><em>...is a great author!</em></p>
  • <p><strong>George Akerlof</strong><br><em>...is a great author!</em></p>
  • <p><strong>Raphael Müller (not an author)</strong><br><em>...is a great author!</em></p>
  • <p><strong>Ed Hardy</strong><br><em>...is a great author!</em></p>
@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>