-
Notifications
You must be signed in to change notification settings - Fork 10
Simplify.Templates
Provides ITemplate
interface and Template
class for working with a text templates.
With Template
you can easily load text templates, localize them, insert data and get generated result.
Available at NuGet as binary package
Loading a template from a file located in a calling assembly directory:
var tpl = Template.Load("TestTemplate.tpl");
The TestTemplate.tpl
file:
<html>
<body>
<div>{SomeVariable}</div>
<div>{Items}</div>
</body>
Setting a data into the template variables:
tpl.Set("SomeVariable", "footext");
tpl.Add("Items", "1")
.Add("Items", "2")
.Add("Items", "3");
Getting the template text:
var str = tpl.Get();
str
will contain:
<html>
<body>
<div>footext</div>
<div>123</div>
</body>
- Loading a template from an assembly embedded resources by:
var tpl = Template.FromManifest("FilePath.Filename.tpl");
- Loading a template from a string:
var tpl = Template.FromString("some string");
orvar tpl = new Template("some string", false);
- Loading a template from a specified assembly embedded resources by:
var tpl = new Template(Assembly.GetAssembly(typeof (SomeClass)), "FilePath.Filename.tpl");
- Loading a template from a full path:
var tpl = new Template("C:\\Templates\\FilePath.Filename.tpl"));
If you want a template to be localizable then you should create a xml file in the same directory as a template file.
Files should be named like FooTemplate.tpl.en.xml
, if a template file name is FooTemplate.tpl
.
You can keep several files for each language, which you want to be localized, for example:
FooTemplate.tpl
FooTemplate.tpl.en.xml
FooTemplate.tpl.de.xml
FooTemplate.tpl.ru.xml
Localization file FooTemplate.tpl.de.xml
example:
<?xml version="1.0" encoding="utf-8"?>`
<items>
<item name="LocalizableVariable" value="Test" />
`</items>
Localization file FooTemplate.tpl.en.xml
example
<?xml version="1.0" encoding="utf-8"?>`
<items>
<item name="LocalizableVariable" value="Test default" />
<item name="LocalizableVariable2">
<a href="#">localizable data default</a>
</item>
`</items>
Template fileFooTemplate.tpl
example:
<html>
<body>
<div>{LocalizableVariable}</div>
<div>{LocalizableVariable2}</div>
</body>
Language codes should be passed via constructor or static factory methods:
var tpl = Template.Load("TestTemplate.tpl", "de", "en");
There are two language code parameters, first is the current language, and seconds is the default language. At the time when a template first loads, then the current language localization file will be inserted into the template, after that the default localization file will be inserted. If some localizable values are not exist in the current localization file, then they will be inserted from the default localization file.
If the language code is not set via constructor or static factory methods, then the Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName
will be used.
Output example:
var tpl = Template.Load("FooTemplate.tpl", "de", "en");
var str = tpl.Get();
str
will contain:
<html>
<body>
<div>Test</div>
<div><a href="#">localizable data default</a></div>
</body>
The MasterTemplate.tpl
file:
<html>
<body>
{Links}
</body>
The FooLinkItem.tpl
file:
<a href="http://foolink/{ItemID}">{MyLinkLabel} {ItemID}</a><br />
The FooLinkItem.tpl.en.xml
file:
<?xml version="1.0" encoding="utf-8"?>`
<items>
<item name="MyLinkLabel" value="My item" />
`</items>
Building links list:
class MyItem
{
public int ID { get; set; }
}
...
var items = new List<MyItem> { {ID = 1}, {ID = 2}, {ID = 3}};
var tpl = Template.Load("Templates/MasterTemplate.tpl");
var itemTpl = Template.Load("Templates/FooLinkItem.tpl");
foreach (var item in items)
{
itemTpl.Set("ItemID", item.ID);
// Adding our generated item data to the master template and rolling it back to initial, but localized state
tpl.Add("Links", itemTpl.GetAndRoll());
}
var str = tpl.Get();
str
will contain:
<html>
<body>
<a href="http://foolink/1">My item 1</a><br />
<a href="http://foolink/2">My item 2</a><br />
<a href="http://foolink/3">My item 3</a><br />
</body>
The MyTemplate.tpl
file:
{Prop1}
{Prop2}
{Prop3}
The model:
public class MyClass
{
public string Prop1 { get; set; }
public int Prop2 { get; set; }
public DateTime Prop3 { get; set; }
}
Setting model to template:
var obj = new MyClass {Prop1 = "Foo", Prop2 = 5, Prop3 = new DateTime(2014, 09, 13)};
var tpl = Template.Load("MyTemplate.tpl");
tpl.Model(obj).With(x => x.Prop3, x => x.ToString("dd.MM.yyyy")).Set();
var str = tpl.Get();
str
will contain:
Foo
5
09.13.2014