Tera Templating Engine
ExTera.RdExTera is an R6 class object that uses extendr to encapsulate Tera's
templating engine. In addition to providing rendering functionality, it acts
as a library to hold templates that may include complex dependencies, a
feature called template "inheritance" in Tera.
Details
A templating engine requires two things:
a
template, as you may have guessed, that includes variables and rendering logic describing where and how to inject data, anda
context, or a set of variables and values to be injected into the template.
Templating syntax is described in the Tera docs.
Methods
Method new()
Create a new ExTera object. Will populate template library with files
in dir if specified.
Usage
ExTera$new(dir = NULL)Arguments
dircharacter scalar, a glob pattern with
*wildcards indicating a potentially nested directory containing multiple file templates. IfNULL(the default), anExTerawith an empty library is initialized. See details for more information.
Details
The glob pattern templates/*.html will match all files with the
.html extension located directly inside the templates folder, while the
glob pattern templates/**/*.html will match all files with the .html
extension directly inside or in a subdirectory of templates. The
default naming convention is to give each template their full relative
path from templates or whatever the directory is called.
Method print()
print method for ExTera object.
Method add_file_templates()
Add templates to library from file paths.
Method add_string_templates()
Add templates to library from character strings.
Method render()
Render specified template to file.
Method render_to_string()
Render specified template to string.
Examples
## ------------------------------------------------
## Method `ExTera$new()`
## ------------------------------------------------
# initialize ExTera with empty library
tera <- ExTera$new()
tera
#>
#> ── ExTera ──
#>
#> Template library:
# initialize ExTera from directory with glob
template_dir <- file.path(tempdir(), "templates")
dir.create(template_dir)
tmp <- file.path(
template_dir,
"hello-world-template.html"
)
writeLines(
text = '<p>Hello {{ x }}. This is {{ y }}.</p>',
con = tmp
)
tera <- ExTera$new(dir = file.path(template_dir, "*.html"))
tera
#>
#> ── ExTera ──
#>
#> Template library:
#> • hello-world-template.html
## ------------------------------------------------
## Method `ExTera$add_file_templates()`
## ------------------------------------------------
tera <- ExTera$new()
writeLines(
'<p>Hello {{ x }}. This is {{ y }}.</p>',
con = file.path(tempdir(), "hello-world.html")
)
writeLines(
'<img src="{{ img_src }}">',
con = file.path(tempdir(), "img-src.html")
)
tera$add_file_templates(
"hello-world" = file.path(tempdir(), "hello-world.html"),
"img-src" = file.path(tempdir(), "img-src.html")
)
tera
#>
#> ── ExTera ──
#>
#> Template library:
#> • hello-world
#> • img-src
## ------------------------------------------------
## Method `ExTera$add_string_templates()`
## ------------------------------------------------
tera <- ExTera$new()
tera$add_string_templates(
"hello-world" = '<p>Hello {{ x }}. This is {{ y }}.</p>',
"img-src" = '<img src="{{ img_src }}">'
)
tera
#>
#> ── ExTera ──
#>
#> Template library:
#> • img-src
#> • hello-world
## ------------------------------------------------
## Method `ExTera$list_templates()`
## ------------------------------------------------
tera <- ExTera$new()
tera$add_string_templates(
"hello-world" = '<p>Hello {{ x }}. This is {{ y }}.</p>',
"img-src" = '<img src="{{ img_src }}">'
)
tera$list_templates()
#> [1] "img-src" "hello-world"
## ------------------------------------------------
## Method `ExTera$render()`
## ------------------------------------------------
tera <- ExTera$new()
tera$add_string_templates(
"hello-world" = '<p>Hello {{ x }}. This is {{ y }}.</p>'
)
outfile <- file.path(tempdir(), "rendered-hello-world.html")
tera$render(
"hello-world",
outfile = outfile,
x = "world",
y = "ExTera"
)
readLines(outfile, warn = FALSE)
#> [1] "<p>Hello world. This is ExTera.</p>"
## ------------------------------------------------
## Method `ExTera$render_to_string()`
## ------------------------------------------------
tera <- ExTera$new()
tera$add_string_templates(
"hello-world" = '<p>Hello {{ x }}. This is {{ y }}.</p>'
)
tera$render_to_string(
"hello-world",
x = "world",
y = "ExTera"
)
#> [1] "<p>Hello world. This is ExTera.</p>"
## ------------------------------------------------
## Method `ExTera$autoescape_on()`
## ------------------------------------------------
tera <- ExTera$new()
tera$add_string_templates(
"hello-world" = '<p>Hello {{ x }}. This is {{ y }}.</p>',
"hello-world.html" = '<p>Hello {{ x }}. This is {{ y }}.</p>'
)
# not recognized as html
tera$render_to_string(
"hello-world",
x = "&world",
y = "an apostrophe, '"
)
#> [1] "<p>Hello &world. This is an apostrophe, '.</p>"
# html
tera$render_to_string(
"hello-world.html",
x = "&world",
y = "an apostrophe, '"
)
#> [1] "<p>Hello &world. This is an apostrophe, '.</p>"
## ------------------------------------------------
## Method `ExTera$autoescape_off()`
## ------------------------------------------------
tera <- ExTera$new()
tera$add_string_templates(
"hello-world.html" = '<p>Hello {{ x }}. This is {{ y }}.</p>'
)
tera$autoescape_off()
tera$render_to_string(
"hello-world.html",
x = "&world",
y = "an apostrophe, '"
)
#> [1] "<p>Hello &world. This is an apostrophe, '.</p>"