pagegen 1.1.3

Page Generator, using templates


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

pagegen

A minimal template based page generation library

How to use:

Generating Strings from a template

// Create an enum, listing all "variables" in a template:
enum Vars{
	First, // this name will be used in the template string
	Second
}

// get a template string, read it from file or whatever
string templateStr = "<tr> <td> %First% </td> <td> %Second% </td> </tr>";

// create a Template class based on that enum
auto rowGen = new Template!Vars(templateStr);

// generate strings from it
string str = rowGen.strGen([
	Vars.First : "foo",
	Vars.Second : "bar"
]);
writeln(str); // <tr> <td> foo </td> <td> bar </td> </tr>

Generating string, with multiple sets of data

In case you want to just append the same template string, you can just pass an array of associative arrays like:

// continued from above code
str = rowGen.strGen([
	[
		Vars.First : "foo",
		Vars.Second : "bar",
	],[
		Vars.First : "foobar",
		Vars.Second : "barfoo"
	]
]);
writeln(str); // <tr> <td> foo </td> <td> bar </td> </tr><tr> <td> foobar </td> <td> barfoo </td> </tr>

Or in case you want to merge the string any other way, use the Glue function:

// continued from above code
// must be a function, not a delagate
void merge(ref string a, string toApp, uint i){
	a ~= (i + 1).to!string ~ " is: " ~ toApp;
}
rowGen.glue = &merge;

str = rowGen.strGen([
	[
		Vars.First : "foo",
		Vars.Second : "bar",
	],[
		Vars.First : "foobar",
		Vars.Second : "barfoo"
	]
]);
writeln(str); // 1 is: <tr> <td> foo </td> <td> bar </td> </tr>2 is: <tr> <td> foobar </td> <td> barfoo </td> </tr>

% character

You can use any other character to enclose variable names in template string like:

auto rowGen = new Template!(someEnum, '$')(" $Foo$ ");
Authors:
  • nafees
Dependencies:
utils
Versions:
1.1.3 2022-Nov-22
1.1.2 2022-Nov-22
1.1.1 2022-Nov-22
1.1.0 2022-Oct-26
1.0.0 2022-Jun-22
Show all 6 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 1 downloads this month

  • 28 downloads total

Score:
0.5
Short URL:
pagegen.dub.pm