Skip to content
This repository was archived by the owner on Jun 30, 2021. It is now read-only.

Simplify.Templates

Alexanderius edited this page Feb 27, 2014 · 19 revisions

Provides ITemplate interface and Template class for working with a text templates.

Basic example

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

Another ways of template load

  • Loading a template from an assembly embedded resources by: var tpl = Template.FromManifest("FilePath.Filename.tpl");
  • Loading a template from a string: var tpl = new Template("some string", false);
  • Loading a template from specified assembly embedded resources by: var tpl = new Template(Assembly.GetAssembly(typeof (SomeClass)), "FilePath.Filename.tpl");
  • Loading a template from full path: var tpl = new Template("C:\\Templates\\FilePath.Filename.tpl"));

Template localization

If you want template to be localizable then you should create xml files in the same directory as a template file. Files should be named like FooTemplate.tpl.en.xml, if 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 example:

<?xml version="1.0" encoding="utf-8"?>`
<items>
	<item name="LocalizableVariable" value="Test" />
	<item name="LocalizableVariable2">
		<a href="#">localizable data</a>
	</item>
`</items>

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.

Clone this wiki locally