Title: | Template Engine Inspired by 'Jinja' |
---|---|
Description: | Template engine powered by the 'inja' C++ library. Users write a template document, using syntax inspired by the 'Jinja' Python package, and then render the final document by passing data from R. The template syntax supports features such as variables, loops, conditions and inheritance. |
Authors: | David Hall [aut, cre, cph] , Lars Berscheid [cph] (Author of bundled inja library), Niels Lohmann [cph] (Author of bundled json library) |
Maintainer: | David Hall <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.1.9000 |
Built: | 2024-10-26 04:26:05 UTC |
Source: | https://github.com/davidchall/jinjar |
Create an object to configure the templating engine behavior (e.g. customize the syntax). The default values have been chosen to match the Jinja defaults.
jinjar_config( loader = NULL, block_open = "{%", block_close = "%}", variable_open = "{{", variable_close = "}}", comment_open = "{#", comment_close = "#}", line_statement = NULL, trim_blocks = FALSE, lstrip_blocks = FALSE, ignore_missing_files = FALSE ) default_config()
jinjar_config( loader = NULL, block_open = "{%", block_close = "%}", variable_open = "{{", variable_close = "}}", comment_open = "{#", comment_close = "#}", line_statement = NULL, trim_blocks = FALSE, lstrip_blocks = FALSE, ignore_missing_files = FALSE ) default_config()
loader |
How the engine discovers templates. Choices:
|
block_open , block_close
|
The opening and closing delimiters
for control blocks. Default: |
variable_open , variable_close
|
The opening and closing delimiters
for print statements. Default: |
comment_open , comment_close
|
The opening and closing delimiters
for comments. Default: |
line_statement |
The prefix for an inline statement. If |
trim_blocks |
Remove first newline after a block. Default: |
lstrip_blocks |
Remove inline whitespace before a block. Default: |
ignore_missing_files |
Ignore |
A "jinjar_config"
object.
The equivalent Jinja class is Environment
, but this term has special
significance in R (see environment()
).
jinjar_config()
jinjar_config()
Loaders are responsible for exposing templates to the templating engine.
path_loader()
loads templates from a directory in the file system.
package_loader()
loads templates from a directory in an R package.
list_loader()
loads templates from a named list.
path_loader(...) package_loader(package, ...) list_loader(x)
path_loader(...) package_loader(package, ...) list_loader(x)
... |
Strings specifying path components. |
package |
Name of the package in which to search. |
x |
Named list mapping template names to template sources. |
A "jinjar_loader"
object.
The loader is an argument to jinjar_config()
.
path_loader(getwd()) package_loader("base", "demo") list_loader(list( header = "Title: {{ title }}", content = "Hello {{ person }}!" ))
path_loader(getwd()) package_loader("base", "demo") list_loader(list( header = "Title: {{ title }}", content = "Hello {{ person }}!" ))
Sometimes you want to render multiple copies of a template, using different
sets of data variables. parse_template()
returns an intermediate version
of the template, so you can render()
repeatedly without re-parsing the
template syntax.
parse_template(.x, .config) ## S3 method for class 'character' parse_template(.x, .config = default_config()) ## S3 method for class 'fs_path' parse_template(.x, .config = default_config())
parse_template(.x, .config) ## S3 method for class 'character' parse_template(.x, .config = default_config()) ## S3 method for class 'fs_path' parse_template(.x, .config = default_config())
.x |
The template. Choices:
|
.config |
The engine configuration. The default matches Jinja defaults,
but you can use |
A "jinjar_template"
object.
render()
to render the final document using data variables.
print()
for pretty printing.
vignette("template-syntax")
describes how to write templates.
x <- parse_template("Hello {{ name }}!") render(x, name = "world")
x <- parse_template("Hello {{ name }}!") render(x, name = "world")
Once a template has been parsed, it can be printed with color highlighting of the templating blocks.
## S3 method for class 'jinjar_template' print(x, ..., n = 10)
## S3 method for class 'jinjar_template' print(x, ..., n = 10)
x |
A parsed template (use |
... |
These dots are for future extensions and must be empty. |
n |
Number of lines to show. If |
input <- '<!DOCTYPE html> <html lang="en"> <head> <title>{{ title }}</title> </head> <body> <ul id="navigation"> {% for item in navigation -%} <li><a href="{{ item.href }}">{{ item.caption }}</a></li> {% endfor -%} </ul> {# a comment #} </body> </html>' x <- parse_template(input) print(x) print(x, n = Inf)
input <- '<!DOCTYPE html> <html lang="en"> <head> <title>{{ title }}</title> </head> <body> <ul id="navigation"> {% for item in navigation -%} <li><a href="{{ item.href }}">{{ item.caption }}</a></li> {% endfor -%} </ul> {# a comment #} </body> </html>' x <- parse_template(input) print(x) print(x, n = Inf)
Data is passed to a template to render the final document.
render(.x, ...) ## S3 method for class 'character' render(.x, ..., .config = default_config()) ## S3 method for class 'fs_path' render(.x, ..., .config = default_config()) ## S3 method for class 'jinjar_template' render(.x, ...)
render(.x, ...) ## S3 method for class 'character' render(.x, ..., .config = default_config()) ## S3 method for class 'fs_path' render(.x, ..., .config = default_config()) ## S3 method for class 'jinjar_template' render(.x, ...)
.x |
The template. Choices:
|
... |
< By default, a length-1 vector is passed as a scalar variable. Use |
.config |
The engine configuration. The default matches Jinja defaults,
but you can use |
String containing rendered template.
parse_template()
supports parsing a template once and rendering multiple
times with different data variables.
vignette("template-syntax")
describes how to write templates.
# pass data as arguments render("Hello {{ name }}!", name = "world") # pass length-1 vector as array render("Hello {{ name.0 }}!", name = I("world")) # pass data programmatically params <- list(name = "world") render("Hello {{ name }}!", !!!params) # render template file ## Not run: render(fs::path("template.txt"), name = "world") ## End(Not run)
# pass data as arguments render("Hello {{ name }}!", name = "world") # pass length-1 vector as array render("Hello {{ name.0 }}!", name = I("world")) # pass data programmatically params <- list(name = "world") render("Hello {{ name }}!", !!!params) # render template file ## Not run: render(fs::path("template.txt"), name = "world") ## End(Not run)