jinja 0.1.0
Jinja template parser
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
Jinja templates
Implementation of the Jinja2 template engine(Python) written in D. Templates can be parsed and evaluated at runtime or compile time as needed.
Installation
Usage
Directly use renderString or renderFile
Render text as is without any variables
renderString("Hello World!"); // Returns "Hello World!"
Render text with variables (Option 1 - Pass local variables)
string name = "Jinja";
renderString!(name)("Hello {{ name }}!"); // Returns "Hello Jinja!"
Render text with variables (Option 2)
JSONValue data;
data["name"] = "Jinja";
renderString("Hello {{ name }}!", data); // Returns "Hello Jinja!"
Render a file without any variables
index.html
<h1>Hello World!</h1>
renderFile("index.html");
Render file with variables
index.html
<h1>{{ title }}</h1>
Option1: Pass local variables
auto title = "Hello Jinja!";
auto content = "....";
renderFile!(title, content)("index.html");
Option2: Use JSONValue data
JSONValue data;
data["title"] = "Hello Jinja!";
data["content"] = "....";
renderFile("index.html", data);
Settings
onMissingkey
// Default. Returns "Hello !"
renderString("Hello {{ name }}!", data, onMissingkey: MissingKey.empty);
renderFile("index.html", data, onMissingkey: MissingKey.empty);
// Returns "Hello {{ name }}!"
renderString("Hello {{ name }}!", data, onMissingkey: MissingKey.passThrough);
renderFile("index.html", data, onMissingkey: MissingKey.passThrough);
// Raises JinjaException
renderString("Hello {{ name }}!", data, onMissingkey: MissingKey.error);
renderFile("index.html", data, onMissingkey: MissingKey.error);
viewsDirectory
renderFile("index.html", data, viewsDirectory: "./templates");
Use single template Jinja instance
auto tmpl = templateFromString("Hello World!");
tmpl.render;
auto tmpl = templateFromString("Hello {{ name }}!");
auto name = "Jinja";
tmpl.render!(name);
auto tmpl = templateFromString("Hello {{ name }}!");
JSONValue data;
data["name"] = "Jinja";
tmpl.render(data);
Render file
auto tmpl = templateFromFile("index.html");
tmpl.render;
auto tmpl = templateFromFile("index.html");
auto name = "Jinja";
tmpl.render!(name);
auto tmpl = templateFromFile("index.html");
JSONValue data;
data["name"] = "Jinja";
tmpl.render(data);
Settings
onMissingkey
// Default. Returns "Hello !"
auto tmpl1 = templateFromString("Hello {{ name }}!", onMissingkey: MissingKey.empty);
auto tmpl2 = templateFromFile("index.html", onMissingkey: MissingKey.empty);
viewsDirectory
auto tmpl = templateFromFile("index.html", viewsDirectory: "./templates");
Use Jinja instance
Jinja view;
view.render!(name)("Hello {{ name }}!");
view.renderString!(name)("Hello {{ name }}!");
view.renderFile!(name)("index.html");
view.render("Hello {{ name }}!", data);
view.renderString("Hello {{ name }}!", data);
view.renderFile("index.html", data);
Settings
Jinja view;
view.settings.onMissingkey = MissingKey.empty;
view.settings.viewsDirectory = "./views"; // Default
view.renderString("Hello {{ name }}!", data);
Template Syntax
Variables
The content inside the {{ and }} will be looked up in the given data.
Hello {{ name }}!
Conditions
{% if username == "admin" %}
Welcome Admin!
{% else %}
Welcome!
{% endif %}
Loop
<ul>
{% for name in names %}
<li>{{ name }}</li>
{% endfor %}
</ul>
Using with Serverino
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ title }}</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
</head>
<body>
<section class="section">
<div class="container">
<h1 class="title">
{{ title }}
</h1>
<p class="subtitle">
{{ subtitle }}
</p>
</div>
</section>
</body>
</html>
app.d
/+ dub.sdl:
dependency "serverino" version="~>0.8.1"
dependency "jinja" version="~>0.1.0"
+/
import jinja;
import serverino;
mixin ServerinoMain;
Jinja view;
static this()
{
view.settings.viewsDirectory = "./views";
}
void simple(Request request, Output output)
{
auto title = "Jinja";
auto subtitle = "Simple Text Template system";
output ~= renderFile!(title, subtitle)("index.html");
}
Run
dub app.d
- 0.1.0 released 15 hours ago
- aravindavk/jinja-d
- MIT
- Copyright © 2024, Aravinda VK <[email protected]>
- Authors:
- Sub packages:
- jinja:parsergen
- Dependencies:
- pegged, pegged:strings, pegged:numbers
- Versions:
-
Show all 2 versions0.1.0 2026-Sep-16 ~main 2026-Sep-17 - Download Stats:
-
-
1 downloads today
-
1 downloads this week
-
1 downloads this month
-
1 downloads total
-
- Score:
- 0.7
- Short URL:
- jinja.dub.pm