Erlang Templating Engines and Dynamic Content Generation

Explore how to generate dynamic content using templating engines in Erlang web applications, focusing on libraries like erlydtl and mustache.erl.

15.4 Templating Engines and Dynamic Content

In the world of web development, generating dynamic content is a fundamental requirement. Erlang, known for its concurrency and fault-tolerance, also provides robust solutions for web development. This section delves into the use of templating engines in Erlang to create dynamic web content, focusing on popular libraries such as erlydtl and mustache.erl. We will explore how these tools can be leveraged to render templates with data, manage layouts, and ensure secure content generation through input sanitization and escaping.

Introduction to Templating Engines

Templating engines are tools that allow developers to separate the presentation layer from the business logic of an application. By using templates, you can define the structure of your HTML documents and dynamically inject data into them. This separation of concerns not only makes your codebase cleaner but also enhances maintainability and scalability.

Key Benefits of Templating Engines

  • Separation of Concerns: Keep HTML structure separate from business logic.
  • Reusability: Use templates across different parts of the application.
  • Maintainability: Easier to update and manage code.
  • Security: Built-in mechanisms for escaping and sanitizing data.

Erlang Templating Libraries

Erlang offers several templating libraries that facilitate the generation of dynamic content. Two of the most popular libraries are erlydtl and mustache.erl.

ErlyDTL

erlydtl is an Erlang implementation of the Django Template Language. It provides a powerful and flexible way to create dynamic web pages.

  • Features:
    • Template inheritance
    • Filters and tags
    • Automatic escaping of variables
    • Support for custom tags and filters

Mustache.erl

mustache.erl is an Erlang implementation of the Mustache templating language, known for its simplicity and logic-less templates.

  • Features:
    • Logic-less templates
    • Support for partials
    • Simple syntax
    • Language agnostic

Rendering Templates with Data

Let’s explore how to render templates using erlydtl and mustache.erl.

Using ErlyDTL

To render a template with erlydtl, you first need to compile the template and then render it with the desired data.

 1% Compile the template
 2{ok, Template} = erlydtl:compile_file("path/to/template.html", []).
 3
 4% Define the data to be injected
 5Data = #{name => "Erlang", version => "23.0"}.
 6
 7% Render the template
 8{ok, Output} = Template:render(Data).
 9
10io:format("~s", [Output]).

Using Mustache.erl

Rendering with mustache.erl involves loading the template and applying it to the data.

 1% Load the template
 2{ok, Template} = file:read_file("path/to/template.mustache").
 3
 4% Define the data
 5Data = #{name => "Erlang", version => "23.0"}.
 6
 7% Render the template
 8Output = mustache:render(Template, Data).
 9
10io:format("~s", [Output]).

Handling Layouts, Partials, and Template Inheritance

Layouts and partials are essential for creating modular and maintainable templates.

Layouts and Partials in ErlyDTL

ErlyDTL supports template inheritance, allowing you to define a base template and extend it in child templates.

  • Base Template (base.html):
 1<!DOCTYPE html>
 2<html>
 3<head>
 4    <title>{% block title %}Default Title{% endblock %}</title>
 5</head>
 6<body>
 7    <header>
 8        <h1>Welcome to My Website</h1>
 9    </header>
10    <main>
11        {% block content %}{% endblock %}
12    </main>
13    <footer>
14        <p>Footer content here</p>
15    </footer>
16</body>
17</html>
  • Child Template (child.html):
1{% extends "base.html" %}
2
3{% block title %}Home Page{% endblock %}
4
5{% block content %}
6    <p>This is the home page content.</p>
7{% endblock %}

Partials in Mustache.erl

Mustache supports partials, allowing you to include reusable template snippets.

  • Main Template (main.mustache):
<h1>{{title}}</h1>
{{> user}}
  • Partial Template (user.mustache):
<p>User: {{name}}</p>
  • Rendering with Partials:
 1% Load templates
 2{ok, MainTemplate} = file:read_file("path/to/main.mustache").
 3{ok, UserPartial} = file:read_file("path/to/user.mustache").
 4
 5% Define data
 6Data = #{title => "Welcome", name => "Erlang"}.
 7
 8% Render with partials
 9Output = mustache:render(MainTemplate, Data, #{user => UserPartial}).
10
11io:format("~s", [Output]).

Importance of Escaping and Input Sanitization

When generating dynamic content, it’s crucial to ensure that the data being injected into templates is properly escaped and sanitized to prevent security vulnerabilities such as Cross-Site Scripting (XSS).

Escaping in ErlyDTL

ErlyDTL automatically escapes variables by default. However, you can use filters to control the escaping behavior.

1<p>{{ user_input|escape }}</p>

Escaping in Mustache.erl

Mustache.erl also escapes HTML by default. To include unescaped content, use triple braces.

<p>{{{ unescaped_content }}}</p>

Encouraging Separation of Concerns

Using templates effectively encourages a clear separation of concerns in your web application. By keeping the presentation logic separate from the business logic, you can create more maintainable and scalable applications.

Try It Yourself

Experiment with the provided code examples by modifying the templates and data. Try creating new templates with different structures and see how the rendering changes. This hands-on approach will deepen your understanding of templating engines in Erlang.

Visualizing Template Rendering

Below is a simple flowchart illustrating the process of rendering a template with data using a templating engine.

    flowchart TD
	    A["Start"] --> B["Load Template"]
	    B --> C["Compile Template"]
	    C --> D["Inject Data"]
	    D --> E["Render Output"]
	    E --> F["Display Content"]
	    F --> G["End"]

Diagram Description: This flowchart represents the steps involved in rendering a template with data. It starts with loading the template, compiling it, injecting data, rendering the output, and finally displaying the content.

References and Further Reading

Knowledge Check

  • What are the benefits of using templating engines in web development?
  • How does erlydtl differ from mustache.erl in terms of features?
  • Why is input sanitization important when rendering templates?
  • How can you use template inheritance in erlydtl?
  • What is the purpose of partials in mustache.erl?

Embrace the Journey

Remember, mastering templating engines is just one step in your journey to becoming an expert in Erlang web development. Keep experimenting, stay curious, and enjoy the process of building dynamic and robust web applications.

Quiz: Templating Engines and Dynamic Content

Loading quiz…
Revised on Thursday, April 23, 2026