hxt is a templating engine that supports any type of file (primarily markup), that closely resembles php syntax.
- Clone this repo and add src/hxt to your classPath (
echo "-cp <path/to/src/hxt>" >> build.hxml). - Add your views directory as a basePath macro in your build file (
echo "--macro hxt.Hxt.basePath('views')" >> build.hxml). - Create a template view. Example:
<html>
<title><?=this.title?></title>
<ul>
<!-- you may either access your variables with `this.<your-var>` or `<your-var>` -->
<?hx
for (i in 0...someOtherVar) {
echo('<li>$i</li>\n');
}
?>
</ul>
</html>
- Create a class wrapper for your view. Example:
@:hxt('about/index.html')
class IndexPage extends Template {
public var title: String = "About Us";
public var someOtherVar: Int = 4;
}Notice that we've declared our class variables the same as those we've referenced in the template. This is important, since Haxe's macro system binds variables by-name at runtime.
- Build your template as follows:
var indexPage: Template = new IndexPage();
// modify your class instance as needed
indexPage.someOtherVar = 10;
// this returns the filled-in template
var contents: String = indexPage.execute();
trace(contents);
/*
* <html>
* <title>About Us</title>
* ...
*/This uses a very primitive, self-invalidating cache, which simply leverages FileSystem.stat(file).mtime.getTime(). This is obviously faster (and more space-efficient) than straight up loading the file into memory, but if anyone's got a better idea...
This project uses an off-standard hxformat.json because style guides are a myth :p
There's no fancy standard-of-quality here, just format your code before PR'ing thanks :3
To generate documentation:
$ # make sure you have `dox` installed
$ # haxelib install dox
$ haxe docs.hxml
$ haxelib run dox -i docs/doc.hxml -o docs/api --include '^hxt'Unit tests are highly encouraged (see test.hxml).
If you're using nix, this project also comes bundled with a handy (but mostly pointless) flake.nix devshell.