HTML Templating
HTML Templating
HTML Templating is considered unstable and may change in the future
Introduction
HTML templating is a technique used to create HTML files with dynamic content. It is a way to separate the HTML structure from the content, and allows us to generate HTML files with different content based on the data we have.
Aurora has built-in support for simple HTML Templating
Usage
To use HTML templating in Aurora, you need to create a template file with the .html
extension. In this file, you can use the {{ }}
syntax to insert dynamic content.
Here is an example of a simple template file:
<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
</head>
<body>
<h1>{{heading}}</h1>
<p>{{content}}</p>
</body>
</html>
In this template file, we have three placeholders: {{ title }}
, {{ heading }}
, and {{ content }}
. These placeholders will be replaced with the actual values when we render the template.
To render the template, we can use the sendHtml
function. Here is an example:
local aurora = require("@aurora/")
aurora.get("/hello", function(req, res)
local data = {
title = "Hello World",
heading = "Welcome to Aurora",
content = "This is a simple example of HTML templating in Aurora."
}
res.html("index.html", data) -- Path is relative to the working directory
end)
aurora.serve() -- http://localhost:8080/hello
In this example, we are rendering the index.html
template file with the data provided in the data
table. The sendHtml
function will replace the placeholders in the template file with the actual values and return the resulting HTML content.
Loops
Loops are still in Development and only support arrays at the moment
You can also use loops in your templates to generate dynamic content. Here is an example:
<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
</head>
<body>
<h1>{{heading}}</h1>
<ul>
{{#foreach item in items}}
<li>{{item}}</li>
{{#endforeach}}
</ul>
</body>
</html>
In this template file, we have a loop that iterates over the items
array and generates a list item for each item in the array.
To render the template with the loop, you can use the following luau code:
local aurora = require("@aurora/")
aurora.get("/hello", function(req, res)
local data = {
title = "Hello World",
heading = "Welcome to Aurora",
items = {"Item 1", "Item 2", "Item 3"}
}
res.html("index.html", data)
end)
aurora.serve() -- http://localhost:8080/hello