This section is just a minimal recap of the HTT templating syntax so you have the proper vocabulary in place and can visually recognize the various parts of the syntax.
To learn how to apply this syntax and write actual templates, see the Quick Start guide.
Finally, you may actually want to see a condensed grammar definition. If so, click here
The majority of the template will probably be literal text. Any text which is not triggering any of the syntax rules below is rendered exactly as you typed it in. This is what permits most templates to still be recognizable to people understanding the eventual output.
% --[[ lua code here ]]--
Lua lines begin with the %
character. The remainder of the line is handled as Lua code and inserted verbatim in the compiled template.
You can write blocks of verbatim Lua code by wrapping it in a %%%
:
%%%
-- lua code here
%%%
Whenever you see {{ ... }}
, it is a Lua expression.
Expressions are evaluated and tostring()
is called on their value and it is this value which is embedded in the output.
<foo>
)
% <my_component>
... content here :)
% </my_component>
Inside a component, ctx
refers to a lua table holding all arguments passed to the component.
You render/use the component using component expression, which looks like this:
{{@ greeting {name = "John"}}}
Here, we call the component greeting
and pass a table to the component, with name
set to "John"
. Note that you can use *any* Lua expression, so long as it evaluates to a table.
Any line which starts with ~>
(after optional indentation).
It may sound technical, but it is simple. A line continuation is just that, a continuation of the prior line.
The following is a definition of the HTT grammar.
// ...
- this is a comment<rule-name> =
- the name of a rule | ...
- the start a definition of the rule (there may be several) . ...
- this is a continuation of the previous rule definition<rule>*
- 0 or more repetitions of rule<rule>+
- 1 or more repetitions of rule<rule>?
- 0 or 1 repetitions of rule'...'
- anything in single quotes is a literal valuetop =
| lua-line
| component
| code
lua-line =
| ws* '%' lua nl
code =
| code-delim nl
. lua nl
. code-delim nl
code-delim =
| ws* '%%%' ws*
component =
| component-open nl
. component-line
. component-close nl
component-open =
| ws* '%' ws+ '<' component_name '>'
component_name =
| [a-zA-Z_][a-zA-Z0-9_]*
component-line =
| lua-line
| text-line
| code
component-close =
| ws* '%' ws+ '</' component_name '>'
text-line =
| ws* line-continuation? element* nl
line-continuation =
| '~>'
element =
| text
| expression
| component-call
// here, 'lua' must be a single-line expression.
expression =
| '{{' lua '}}'
// here 'lua-table-expr' is some single-line lua expression
// which evaluates to a Lua table.
component-call =
| '{{@' ws* component_name ws+ lua-table-expr '}}'
ws =
| \r
| \t
| ' '
nl =
| \r? \n