The compiler of Lumen templates. A template is Lua code in which markup may appear wherever an expression may:

local Card = Lumen.require('card')

return function(props)
  return <view style={{ padding = 8, gap = 4 }}>
    <text style={{ font = 'text_normal' }}>Hello, {props.name}!</text>
    {Lumen.map(props.items, function(item)
      return <Card key={item.id} item={item} />
    end)}
    {props.loading and <text>Loading...</text>}
  </view>
end

The compiler turns every piece of markup into a call to Lumen.element and leaves the rest of the code as it is, newlines included, so that the line numbers in error messages match the template. The rules are:

  • A tag whose name starts with a lower case letter is an intrinsic element (view, text, button, ...) or the ID of another template, and is passed on as a string. A tag whose name starts with an upper case letter, or has a dot in it, is a component: a Lua expression such as Card or Flux.Panels.Card that is in scope.
  • <>...</> is a fragment: its children are put straight into the parent.
  • An attribute is name="text", name='text', name={expression} or just name, which stands for true. {...expression} among the attributes spreads a table of props into the element, in the order written.
  • Children are text, nested elements and {expression} splices. An expression may yield an element, a string or a number (text), a list of any of these, or nil and false, which are left out. Text that spans several lines is trimmed line by line and joined with spaces, and the character entities &amp;, &lt;, &gt;, &quot;, &apos;, &nbsp;, &#NN; and &#xHH; are decoded.
  • <!-- ... --> is a comment.

A < is taken as the start of markup when it is followed by a name or by > and the token before it cannot end an operand: a < b and #list < 3 remain comparisons, while return <view/>, { <view/> }, x = cond and <view/> and f(<view/>) are markup.

Summary

Functions

Compiles a template to Lua code.

Functions

Lumen.Compiler.compile(source, name)

shared packages/lumen/lib/sh_compiler.lua:675

Compiles a template to Lua code. The code is the template with every piece of markup replaced by a call to Lumen.element; it has the same number of lines as the template. Raises an error that names the template and the line when the markup is malformed.

Lumen.Compiler.compile('return <text color="red">Hi, {name}</text>', 'greeting')
-- return Lumen.element("text", { color = "red" }, { "Hi, ", (name) })

Parameters

  • source String template source
  • name String optional, defaults to 'lumen' name of the template, used in error messages

Returns

  • String Lua code

Defined in