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 6/16/2024 2:14 AM
  • Template: It's already [DateTime:Now|yyyy-MM-dd HH:mm:ss]
    Result: It's already 2024-06-16 02:14:20
  • 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:
    <strong>Hello there 👋🏾</strong> <br> <em>This is a simple dic-sample</em>
  • Result Raw:
    Hello there 👋🏾
    This is a simple dic-sample
@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:
    <strong>Douglas Adams</strong> <br> <em>Fav. Number: 41.99</em>
  • Result Raw:
    Douglas Adams
    Fav. Number: 41.99
@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:
    <p><strong>Douglas Adams</strong><br><em>...is a great author!</em></p>
  • Result Raw HTML using Html(..., tweak: ...)

    Douglas Adams
    ...is a great author!

@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

  • Douglas Adams
    ...is a great author!

  • Terry Pratchett
    ...is a great author!

  • Neil Gaiman
    ...is a great author!

  • George Akerlof
    ...is a great author!

  • Raphael Müller (not an author)
    ...is a great author!

  • Ed Hardy
    ...is a great author!

@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

  • Douglas 💫 Adams
    is an awesome 🦾 author!

  • Terry 💫 Pratchett
    is an awesome 🦾 author!

  • Neil Gaiman
    ...is a great author!

  • George Akerlof
    ...is a great author!

  • Raphael Müller (not an author)
    ...is a great author!

  • Ed Hardy
    ...is a great author!

@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>