Learn R Programming

jinjar (version 0.3.1)

render: Render a template

Description

Data is passed to a template to render the final document.

Usage

render(.x, ...)

# S3 method for character render(.x, ..., .config = default_config())

# S3 method for fs_path render(.x, ..., .config = default_config())

# S3 method for jinjar_template render(.x, ...)

Value

String containing rendered template.

Arguments

.x

The template. Choices:

  • A template string.

  • A path to a template file (use fs::path()).

  • A parsed template (use parse_template()).

...

<dynamic-dots> Data passed to the template.

By default, a length-1 vector is passed as a scalar variable. Use I() to declare that a vector should be passed as an array variable. This preserves a length-1 vector as an array.

.config

The engine configuration. The default matches Jinja defaults, but you can use jinjar_config() to customize things like syntax delimiters, whitespace control, and loading auxiliary templates.

See Also

  • parse_template() supports parsing a template once and rendering multiple times with different data variables.

  • vignette("template-syntax") describes how to write templates.

Examples

Run this code
# 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
if (FALSE) {
render(fs::path("template.txt"), name = "world")
}

Run the code above in your browser using DataLab