Title: | A System for Working with Templates |
---|---|
Description: | Provides tools to work with template code and text in R. It aims to provide a simple substitution mechanism for R-expressions inside these templates. Templates can be written in other languages like 'SQL', can simply be represented by characters in R, or can themselves be R-expressions or functions. |
Authors: | Sebastian Warnholz [aut, cre] |
Maintainer: | Sebastian Warnholz <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.4.0 |
Built: | 2024-11-10 04:15:02 UTC |
Source: | https://github.com/wahani/templates |
tmpl
is the constructor function for template objects.
tmpl(.t, ...) ## S3 method for class 'character' tmpl(.t, ..., .envir = parent.frame()) ## S3 method for class 'formula' tmpl(.t, ...) ## S3 method for class 'tmpl' tmpl(.t, ...) ## S3 method for class 'function' tmpl(.t, ...)
tmpl(.t, ...) ## S3 method for class 'character' tmpl(.t, ..., .envir = parent.frame()) ## S3 method for class 'formula' tmpl(.t, ...) ## S3 method for class 'tmpl' tmpl(.t, ...) ## S3 method for class 'function' tmpl(.t, ...)
.t |
something that can be interpreted as template. See defined methods for options. |
... |
(name = value | name ~ value) name-value expressions passed on to tmplUpdate |
.envir |
(environment) the environment in which template snippets are
evaluated. For |
Objects of class tmpl
are stored as a character of length
one. They can contain 'snippets' to be evaluated. These snippets are
identified by an opening {{
and closing }}
. The
environment in which they are evaluated is stored in the object. They can
be further augmented by supplying arguments in ...
.
tmpl("Hi {{ toupper(a) }}!", a = "there") tmpl( ~ {y <- {{ a }}}, a ~ x + 1) tmpl(function(x) {{ a }} + x, a ~ 1)
tmpl("Hi {{ toupper(a) }}!", a = "there") tmpl( ~ {y <- {{ a }}}, a ~ x + 1) tmpl(function(x) {{ a }} + x, a ~ 1)
Functions operating on tmpl objects. They can be updated and / or evaluated as an expression.
tmplUpdate(.t, ...) ## S3 method for class 'tmpl' tmplUpdate(.t, ...) ## S3 method for class 'function' tmplUpdate(.t, ...) tmplEval(.t, ..., .envir = new.env(parent = parent.frame())) tmplAsFun(.t, ...)
tmplUpdate(.t, ...) ## S3 method for class 'tmpl' tmplUpdate(.t, ...) ## S3 method for class 'function' tmplUpdate(.t, ...) tmplEval(.t, ..., .envir = new.env(parent = parent.frame())) tmplAsFun(.t, ...)
.t |
(tmpl) and object of class |
... |
(name = value | name ~ value) name-value expressions used to
update the snippets in |
.envir |
(environment) the environment in which the template is evaluated |
tmplUpdate
will evaluate all snippets in a template. Objects are
searched for in the list of arguments supplied as ...
and the
environment of the template. The results are substituted within the snippets.
tmplEval
will evaluate the template in place or in the specified
environment after substituting the elements in ...
.
tmpl("This is {{ a }} very similar to {{ b }}", a = "actually", b = "sprintf") tmpl("But I consider it to be ({{ sprintf('%i', a) }}) orthogonal", a = 1.0) tmpl("and ({{ sprintf('%i', b) }}) with a different scope:", b = 2.0) tmpl("SELECT {{ var }} FROM {{ table }} WHERE {{ condition }};", var = "someVar", table = "someTable", condition = "primaryKey = 1") template <- tmpl("cat({{ toupper(x) }})") tmplUpdate(template, x ~ "hi") tmplEval(template, x ~ "hi")
tmpl("This is {{ a }} very similar to {{ b }}", a = "actually", b = "sprintf") tmpl("But I consider it to be ({{ sprintf('%i', a) }}) orthogonal", a = 1.0) tmpl("and ({{ sprintf('%i', b) }}) with a different scope:", b = 2.0) tmpl("SELECT {{ var }} FROM {{ table }} WHERE {{ condition }};", var = "someVar", table = "someTable", condition = "primaryKey = 1") template <- tmpl("cat({{ toupper(x) }})") tmplUpdate(template, x ~ "hi") tmplEval(template, x ~ "hi")