Created
May 26, 2020 17:11
-
-
Save gfoidl/cc105f38c1cbc4275384dc901f105c4b to your computer and use it in GitHub Desktop.
.NET Core Text-template preprocessor / rendering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template debug="true" hostspecific="false" language="C#" #> | |
<html> | |
<head> | |
<style> | |
.myclass-1 { | |
margin-bottom: 1em; | |
background-color: lime; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="myclass-1"> | |
Table generated by template. | |
</div> | |
<div> | |
<table> | |
<thead> | |
<tr> | |
<th>Id</th> | |
<th>Name</th> | |
</tr> | |
</thead> | |
<tbody> | |
<# foreach (Person person in this.Model.Persons) | |
{ #> | |
<tr> | |
<td><#= person.Id #></td> | |
<td><#= person.Name #></td> | |
</tr> | |
<# } #> | |
</tbody> | |
</table> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net5.0</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<None Update="HtmlTemplate.tt" Generator="TextTemplatingFilePreprocessor" LastGenOutput="HtmlTemplate.cs" /> | |
</ItemGroup> | |
<ItemGroup> | |
<PackageReference Include="System.CodeDom" Version="4.7.0" /> | |
</ItemGroup> | |
<ItemGroup> | |
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" /> | |
</ItemGroup> | |
<ItemGroup> | |
<Compile Update="HtmlTemplate.cs"> | |
<DesignTime>True</DesignTime> | |
<AutoGen>True</AutoGen> | |
<DependentUpon>HtmlTemplate.tt</DependentUpon> | |
</Compile> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
namespace HtmlTemplateRenderer | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var model = new Model { Persons = GetPersons().ToList() }; | |
var template = new HtmlTemplate(model); | |
string rendered = template.TransformText(); | |
File.WriteAllText("index.html", rendered); | |
} | |
private static IEnumerable<Person> GetPersons() | |
{ | |
yield return new Person { Id = 1, Name = "Anton" }; | |
yield return new Person { Id = 2, Name = "Berta" }; | |
} | |
} | |
partial class HtmlTemplate | |
{ | |
public Model Model { get; } | |
public HtmlTemplate(Model model) => this.Model = model; | |
} | |
public class Model | |
{ | |
public List<Person> Persons { get; set; } | |
} | |
public class Person | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment