Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hxt - a simple, portable Haxe templating engine

hxt is a templating engine that supports any type of file (primarily markup), that closely resembles php syntax.

Usage

  1. Clone this repo and add src/hxt to your classPath (echo "-cp <path/to/src/hxt>" >> build.hxml).
  2. Add your views directory as a basePath macro in your build file (echo "--macro hxt.Hxt.basePath('views')" >> build.hxml).
  3. 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>
  1. 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.

  1. 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>
 *   ...
 */

Notes

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

Development

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.

About

A simple, portable templating engine written in Haxe, for Haxe.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages