-
Notifications
You must be signed in to change notification settings - Fork 3
Embedding Lua
Lua can be embedded in .html, .md and .css files by default.
A block of code exists between {{ and }}, which can be literally anywhere in the file.
Code is not context sensitive, other than what type of file it is in (e.g. HTML entities may get encoded in .html and .md files, but not in .css files).
<h1>Questions for year {{ os.date("%Y") }}</h1>
<section>
{{
echoRaw("<h2>")
echo("Is 5 < 9 still true?")
echoRaw("</h2>")
}}
</section>There are two main kinds of code blocks: Those that output (or "echo") a value, and those that just contain code to run.
If the block contains a value expression then the result is echoed:
<p>86 + 47 = {{ 86 + 47 }}</p>
<p>My name is {{ getMyName() .. " the 3rd" }}</p>
<p>Random double-digit number: {{
math.random( -- Code can span multiple lines.
10, 99
)
}}</p>
{{ nil --[[ Nil values don't echo anything. ]] }}
{{ print("Hello!") --[[ The print function returns nothing, so nothing is echoed here either. ]] }}Otherwise the code just runs:
<p>
Have you seen
{{
print("Just counting to 4...")
for i = 1, 4 do
print(i)
end
}}
the cat?
</p>There is a third special kind of block that can be used for outputting percent-encoded URLs. The block should contain a relative or absolute URL (without quotes or anything else). These two blocks are equal:
<a href="{{ /blog/2008/smörgåsbord/ }}">The Grand Smörgåsbord</a>
<a href="{{ echo(url('/blog/2008/smörgåsbord/')) }}">The Grand Smörgåsbord</a>Control structures in templates behave pretty much like in normal Lua, but here are some additions.
To use a "templateified" control structure put the keyword right after {{:
Normal if:
{{
if foo == "bar" then
echo("Foo is bar!")
end
}}
Templateified if:
{{ if foo == "bar" }}
Foo is bar!
{{ end }}
{{ if foo == "bar" }}
Extra spaces are fine.
{{ end }}
Several versions of a simplified for statement:
{{ for 3 }}
- Loop forwards from 1 to 3: {{ i }}
{{ end }}
{{ for -3 }}
- Loop backwards from 3 to 1: {{ i }}
{{ end }}
{{ fori dog in data.dogs }}
- {{ i }}: {{ dog.name }}
{{ end }}
{{ fori data.dogs }}
- {{ i }}: {{ it.name }}
{{ end }}A code block at the top of the page is commonly used as a place for setting page properties. Example:
{{
page.title = "Cool Dogs"
page.publishDate = "1999-06-29 15:00"
page.layout = "minimal"
}}
## Golder Retriever
Blah blah blah.
## Jack Russel Terrier
Blah blah blah.- Home
- Command Line
- Site Configuration
- Embedding Lua
- Constants
- Functions
- Objects
- Other Modules and Information