Get ZetTemplate at SourceForge.net. Fast, secure and Free Open Source software downloads

ZetTemplate

Readme

Overview

ZetTemplate is a very simple, lightweight, templating system for C++.

It was designed as an easy to way to allow internationalisation of text used in games, however can also be used for any variable substitution situations.

It supports UTF-8 Unicode, and can substitute any type that can be parsed into a std::stringstream (this can be extended by specialising the templated ZetValue class).

Download and Support

Project download and support on SourceForge

Variables

Variables in ZetTemplate are declared using @'s, for example, @name@ is a variable called name.

If you need to use a literal @ in a template, it must be escaped using an additional @, for example, dave@@somecompany.com becomes dave@somecompany.com once the template has been rendered.

Rendering

Rendering is the term given to the process that produces the final (rendered) output from a template and a set of variables.

The same template can be rendered multiple times using different data without having to reload the template.

Example - Template from a string

ZetTemplate can either create a template from a string, or from a file. This example will show how to use ZetTemplate when using a string.

The code below creates an instance of ZetTemplate using a string literal, then creates and populates an instance of ZetValues, before rendering the template into the std::cout stream.

It then updates the variables and re-renders the same template using different data.

		#include < iostream >
		#include "ZetTemplate.h"
		using namespace zettemplate;

		int main()
		{
			// Create a template from a string
			ZetTemplate templ("Hello @name@!\tWelcome to @place@", ZetTemplate::MODE_STRING);
			ZetValues values;

			// Set the values to use in the template, then render it using them
			values.set("name",	"World");
			values.set("place", "Hell");
			std::cout << templ.render(values) << std::endl;
			
			// Update the values and render again
			values.set("name",	"Bob");
			std::cout << templ.render(values) << std::endl;

			// Update the values and render again
			values.set("name",	"Jamie");
			std::cout << templ.render(values) << std::endl;
			
			return 0;
		}
	

The output from running this application is shown below.

		Hello World!    Welcome to Hell
		Hello Bob!      Welcome to Hell
		Hello Jamie!    Welcome to Hell
	

Example - Template from a file

This example shows how to load in a template from a file. The input file used in this example is shown below and is saved as a UTF-8 file.

		UK (ANSI) String: 			Hello @name@, please contact dave@@somecompany.com
		Japanese (UTF8) String: 	こんにちは @name@、dave@@somecompany.com をご連絡ください

		UK Date: @day@/@month@/@year@
		US Date: @month@/@day@/@year@
	

The code below creates an instance of ZetTemplate using a filename, then creates and populates an instance of ZetValues, before rendering the template back out as a UTF-8 file.

		#include < fstream >
		#include "ZetTemplate.h"
		using namespace zettemplate;

		void writeUTF8File(const std::string&, const std::string&);

		int main()
		{
			// Create a template from a UTF8 file (skipping the UTF8 BOM)
			ZetTemplate templFile("InputUTF8.txt", ZetTemplate::MODE_FILE, true);
			ZetValues valuesFile;

			// Set the values to use in the template, then render it back out to a file
			valuesFile.set("day",	23);
			valuesFile.set("month", 4);
			valuesFile.set("year",	2010);
			valuesFile.set("name",	"Jamie");
			writeUTF8File("OutputUTF8.txt", templFile.render(valuesFile));

			return 0;
		}

		void writeUTF8File(const std::string &filename, const std::string &data)
		{
			static const unsigned char UTF8BOM[3] = { 0xEF, 0xBB, 0xBF };
			std::ofstream fout(filename.c_str(), std::ios::out | std::ios::binary);
			if(fout.is_open())
			{
				fout.write(reinterpret_cast< const char* >(UTF8BOM), 3);
				fout.write(data.c_str(), data.length());
				fout.close();
			}
		}
	

The contents of the output file is shown below.

		UK (ANSI) String: 			Hello Jamie, please contact dave@somecompany.com
		Japanese (UTF8) String: 	こんにちは Jamie、dave@somecompany.com をご連絡ください

		UK Date: 23/4/2010
		US Date: 4/23/2010
	

History

License

ZetTemplate.

Copyright © 2010 Jamie Dale.

This software is licensed under the MIT license.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software withoutrestriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.