ithril 1.3.1

Templates for haxe. Compiles to HTML or Mithril views.

Released 2016-12-14.

To install, run:

haxelib install ithril 1.3.1

See using Haxelib in Haxelib documentation for more information.

Current version1.3.1
StatisticsInstalled 188 times
Tags html, js, template, web


Build Status

Templates for haxe. Compiles to HTML or Mithril views.

function () [
			(li > 'Some')
			(li > 'List items')
			(list => item)
				(li > item.title)
			(input[type="text"] (value = "Text value", onfocus = focus))
			(input[type="checkbox"][required]) ['Check this']


Ithril views must be declared inside brackets. If used in a method they will always return.


Any html element can be expressed in parentheses:

The class can be set as in a css selector:

An id can be added to the selector with + (as # wouldn't be valid haxe syntax):

Attributes can be used inside the selector:

Attributes can also be expressed separately:
(img (src="img.jpg", alt=""))
(img ({src: "img.jpg", alt: ""}))
(img (aFunctionCallReturningAttributes()))


A shortcut for defining one child:
(h1 > 'My title')

More than one child can simply be nested by using indentation:

			(a[href=""] > 'Haxe')
			(a[href=""] > 'Github')

Inline expressions

Any expression can be used inside brackets:

	['A string for example']


If/else can be used inside templates ($elseif is on the todo list):

($if (bigTitle))
	(h1 > 'Big')
	(h2 > 'Not that big')

For loop

(link in links)
	(a (href=link.url, target='_blank') > link.title)


Following syntax can be used for any object (in this case links) with a map method:

(links => link)
	(a (href=link.url, target='_blank') > link.title)


Custom components can be created by extending ithril.Component.
A component can then be used in your view like any other element:
(MyComponent (attr1=1, attr2=2))


A component's attributes are type checked, so the example above would have to be defined like this:
class MyComponent extends Component<{attr1: Int, attr2: Int}>

If no attributes are needed, you don't have to define a type parameter:
class MyComponent extends Component

You can also require an instance:
class Label extends Component<String>
Usage would be:
(Label ('My text'))

State can be accessed inside the component:

class MyComponent extends Component<{attr1: Int, attr2: Int}> {
	public function view() [
			(span.attr > 'Attribute 1'+state.attr1)
			(span.attr > 'Attribute 2'+state.attr2)


Children of a Component can be used however you like (children => child is map syntax):

class List extends Component {
	public function view() [
			(children => child)
				(li > child)

Which can be used like this:

	(span > 'A')
	(span > 'B')
	(span > 'C')

And would output:



A component is cached while it's in view. You can perform an action the moment it's created or destroyed (no longer in view) by using mount and unmount. This can be useful to, for example, set/unset listeners. These methods are only called on the client side.

class Container extends Component {
	override public function mount()
		js.Browser.window.addEventListener('resize', resize);

	override public function unmount()
		js.Browser.window.removeEventListener('resize', resize);

	function resize(e)
		trace('Resize event!');


Components can be rendered by passing an instance to mithril:
M.mount(js.Browser.document.body, component);

Or may be rendered as html (string):


Any of your class methods can use ithril syntax if you either implement ithril.IthrilView or extend ithril.Component.

class Web extends ithril.Component {
	public function layout() [

	public function view() [
			(h1 > 'Ithril example')
			(p > 'Hello world')

	public static function main() {
		// On the server
		#if !js
		Sys.print(ithril.HTMLRenderer.render(new Web().layout()));
		// On the client
		mithril.M.mount(js.Browser.document.body, new Web());


Everything gets compiled to simple objects, using this notation so the output can be used directly with Mithril. Use ithril.HTMLRenderer.render to render the output to a string.