lenguar wraps lengua — a git-backed
library for templated text and snippets — via Rust FFI (extendr), so
every function here runs the same lengua-core engine as the
standalone lengua CLI, just without a subprocess in
between.
A library is a .lengua/ directory of sources
Every lenguar function takes a path to a
library: a directory containing .lengua/, which
holds one or more named sources — each its own git repo with a
templates/ folder inside it. Create one with
lq_init(), which sets up the library’s first source (named
"local" by default):
store <- withr::local_tempdir()
lq_init(store)A library only ever needs more than one source if you’re pooling
templates from elsewhere — see Pooling more than one
source below. Everything up to that point works exactly as if
path were still a single plain store.
Adding templates
lq_add() writes a template body plus optional
frontmatter (title, and any other fields) and
commits the change:
lq_add(
store, "letters/thank-you.md",
"Dear {{ name }},\n\nThank you for {{ reason }}.",
title = "Thank You",
fields = c(tone = "formal")
)Calling lq_add() again for the same name
records a new version — lenguar doesn’t have a separate versioning
mechanism, it’s just git commits (see History
below).
Rendering
lq_get() substitutes {{ variable }}
placeholders (minijinja/Jinja2
syntax) using the named character vector passed as vars.
See lengua-core’s templating
guide for the full syntax lenguar inherits unchanged (filters,
conditionals, loops, arithmetic, escaping) — lenguar doesn’t add or
restrict anything here, it’s the same render() call
underneath.
lq_get(store, "letters/thank-you.md", vars = c(name = "Ada", reason = "your thoughtful review"))
#> [1] "Dear Ada,\n\nThank you for your thoughtful review."Pass raw = TRUE to get the unrendered body back instead
(useful for round-tripping a template into another store, or into an
editor).
Listing and searching
lq_list() returns every template as a data frame;
lq_search() filters by frontmatter field (every
fields pair must match — an AND, not an OR):
lq_add(store, "letters/casual-note.md", "Hey {{ name }}!", fields = c(tone = "casual"))
lq_list(store)
#> lengua: 2 templates
#> name title source
#> 1 letters/casual-note.md <NA> local
#> 2 letters/thank-you.md Thank You local
lq_search(store, c(tone = "formal"))
#> lengua: 1 template
#> name title source
#> 1 letters/thank-you.md Thank You localBoth return a lenguar_templates data frame, so anything
that works on a data frame (subsetting, dplyr::filter(), …)
works here too.
History
Because every lq_add() is a git commit,
lq_log() and lq_diff() give you real history
for free:
lq_add(store, "letters/thank-you.md",
"Dear {{ name }},\n\nThank you so much for {{ reason }}.",
title = "Thank You", fields = c(tone = "formal")
)
lq_log(store, "letters/thank-you.md")
#> lengua log: 2 commits
#> commit message
#> 1 60e39cc9562ce872f2c0808d8635e7fdf7fa4dfd add/update template
#> 2 911f90b5a73602be9b09303cebadc1b69b032555 add/update template
lq_diff(store, "letters/thank-you.md")
#> ---
#> title: Thank You
#> tone: formal
#> ---
#>
#> Dear {{ name }},
#>
#> - Thank you for {{ reason }}.
#> + Thank you so much for {{ reason }}.Tagging revisions
lq_tag() points a named tag at a specific revision of a
template — the current one, or (via rev) a prior one, which
is how you retroactively tag a revision after the fact:
lq_add(store, "letter.md", "We had so much fun.", message = "past tense")
lq_add(store, "letter.md", "We will have so much fun.", message = "future tense")
lq_tag(store, "letter.md", "tense-future")
lq_tag(store, "letter.md", "tense-past", rev = "HEAD~1")
lq_tag_list(store, "letter.md")
#> lengua tag list: 2 tags
#> tag commit
#> 1 tense-future 82d5ab3142ff4c188ac31ef4d46acb022a88a8d9
#> 2 tense-past 138f24050af3bbe876d59e4959470fe5c2870321These are lenguar’s own tags
(refs/lengua/tags/<template>/<tag>), not git
tags — they’re scoped per template, so the same tag name can
independently exist on several templates. A tag name works anywhere
lq_get()’s rev or lq_diff()’s
from/to accept a revision:
Consuming an existing library
Most of the time you’re not building a template library from scratch
— you’re using one a team already maintains. lq_init()’s
from_dir/from_repo arguments adopt an existing
store instead of starting empty, mirroring the lengua CLI’s
init --from-dir/--from-repo.
from_dir (and from_repo when it names a local
path) copies the source on the filesystem, preserving its full history
and tags:
copy <- withr::local_tempdir()
lq_init(copy, from_dir = store)
lq_log(copy, "letter.md")
#> lengua log: 2 commits
#> commit message
#> 1 82d5ab3142ff4c188ac31ef4d46acb022a88a8d9 future tense
#> 2 138f24050af3bbe876d59e4959470fe5c2870321 past tense
lq_tag_list(copy, "letter.md")
#> lengua tag list: 2 tags
#> tag commit
#> 1 tense-future 82d5ab3142ff4c188ac31ef4d46acb022a88a8d9
#> 2 tense-past 138f24050af3bbe876d59e4959470fe5c2870321from_repo against a real remote host clones over the
network instead:
lq_init(store, from_repo = "acme-org/team-templates")
lq_init(store, from_repo = "acme-org/team-templates/letters@v2")See lengua’s own docs on consuming an existing library for the full walkthrough, including GitHub Enterprise hosts and importing a subdirectory.
Pooling more than one source
lq_init() adopts a store as your library’s
first source. To add a second (or third, …) without starting a
new library, use lq_fetch() — same adoption arguments,
appended instead of replacing:
team <- withr::local_tempdir()
lq_init(team)
lq_add(team, "letters/thank-you.md", "Dear {{ name }}, thank you!", title = "Thank You")
pooled <- withr::local_tempdir()
lq_init(pooled)
lq_fetch(pooled, from_dir = team, name = "team")The new source becomes the highest-precedence one:
lq_get()/lq_list()/lq_search()
(with no source) merge every source together, and if two
sources define the same template name, the most recently fetched
one wins — lq_fetch()’s result carries any
collision as $warnings, and
lq_list()/lq_search()’s source
column always shows which source a result actually came from. Pass
source to
lq_get()/lq_list()/lq_search() to
bypass the merge and read one source directly — including one that’s
currently shadowed — and to
lq_add()/lq_log()/lq_diff()/lq_tag()/lq_tag_list()/lq_tag_rm()
once a library has more than one source, since there’s no longer a
single default source to target.
lq_update() refreshes one source
(source = "team") or every source from its recorded origin,
fast-forward only — the same semantics as
git pull --ff-only, never a hard reset. A source with
nothing to update from (the library’s own local source, or
one imported with subdir) reports
status = "not-updatable", not an error:
lq_update(pooled)
#> lengua update: 2 sources
#> source status
#> 1 local not-updatable
#> 2 team up-to-date
#> detail
#> 1 source 'local' can't be updated: a local source has no origin to update from
#> 2 <NA>A library built entirely from lq_fetch()ed sources, with
no local source of its own, is a completely normal way to
use lenguar — consuming shared templates never requires calling
lq_init()/lq_add() without adoption arguments
at all.
Advanced End-User workflows in R
3-tier multi-source layering
You can layer corporate, department, and project templates in a single R session:
# 1. Base corporate standards
corp <- withr::local_tempdir()
lq_init(corp)
lq_add(corp, "welcome.md", "Welcome to Acme Corp, {{ name }}!", fields = c(tier = "corp"))
lq_add(corp, "legal/nda.md", "Mutual NDA for {{ recipient }}.", fields = c(tier = "corp"))
# 2. Department sales templates (overrides welcome.md)
sales <- withr::local_tempdir()
lq_init(sales)
lq_add(sales, "welcome.md", "Welcome to the Sales Team, {{ name }}!", fields = c(tier = "sales"))
lq_add(sales, "sales/pitch.md", "Proposal for {{ product }}.", fields = c(tier = "sales"))
# 3. Project-specific library
workspace <- withr::local_tempdir()
lq_init(workspace, from_dir = corp, name = "corp")
lq_fetch(workspace, from_dir = sales, name = "sales")
# Unscoped resolution gets the winning sales version:
lq_get(workspace, "welcome.md", vars = c(name = "Ada"))
#> [1] "Welcome to the Sales Team, Ada!"
# Explicit source access retrieves the underlying corporate copy:
lq_get(workspace, "welcome.md", vars = c(name = "Ada"), source = "corp")
#> [1] "Welcome to Acme Corp, Ada!"Parameterized document & report generation
Use lq_get() in R vector pipelines or
purrr/apply workflows:
recipients <- data.frame(
name = c("Alice", "Bob", "Charlie"),
product = c("Widget Pro", "Analytics Suite", "Cloud Storage"),
stringsAsFactors = FALSE
)
pitches <- vapply(seq_len(nrow(recipients)), function(i) {
row <- recipients[i, ]
lq_get(workspace, "sales/pitch.md", vars = c(name = row$name, product = row$product))
}, character(1))
head(pitches, 2)
#> [1] "Proposal for Widget Pro." "Proposal for Analytics Suite."Advanced Storekeeper workflows in R
Storekeepers can manage organization stores and automate quality assurance directly in R.
Release tagging and version diffs
# Release v1.0.0
lq_add(corp, "reports/annual.md", "Annual report v1.0 outline.", message = "v1.0.0")
lq_tag(corp, "reports/annual.md", "v1.0.0")
# Release v2.0.0
lq_add(corp, "reports/annual.md", "Annual report v2.0 with updated KPIs.", message = "v2.0.0")
lq_tag(corp, "reports/annual.md", "v2.0.0")
# Inspect differences across releases
lq_diff(corp, "reports/annual.md", from = "v1.0.0", to = "v2.0.0")
#> ---
#> {}
#> ---
#>
#> - Annual report v1.0 outline.
#> + Annual report v2.0 with updated KPIs.Automated CI/CD validation pattern
Storekeepers can write testthat tests to ensure all
templates in a store meet schema requirements and render cleanly:
validate_store <- function(store_path, mock_vars = c(name = "Test", product = "Demo", recipient = "Partner")) {
templates <- lq_list(store_path)
for (i in seq_len(nrow(templates))) {
name <- templates$name[i]
title <- templates$title[i]
stopifnot("Missing title" = nzchar(title))
raw <- lq_get(store_path, name, raw = TRUE)
stopifnot("Empty template" = nzchar(raw))
# Dry-run render test
rendered <- lq_get(store_path, name, vars = mock_vars)
stopifnot("Render failed" = nzchar(rendered))
}
TRUE
}
validate_store(workspace)
#> [1] TRUEError handling
Every lenguar function that talks to the underlying store raises a
classed condition on failure — lenguar_store_error for
problems from lengua-core (a missing template, an invalid revision, …)
and lenguar_input_error for bad arguments caught on the R
side (an unnamed fields/vars vector, an empty
search() filter). Both inherit from
lenguar_error, so you can catch either specifically or both
at once:
tryCatch(
lq_get(store, "does-not-exist.md"),
lenguar_store_error = function(e) cat("store error:", conditionMessage(e), "\n")
)
#> store error: lengua get failed: template not found: does-not-exist.mdWhere to next
See ?lq_add, ?lq_get,
?lq_list, ?lq_search, ?lq_log,
?lq_diff, ?lq_tag, ?lq_tag_list,
?lq_tag_rm, ?lq_init, ?lq_fetch,
and ?lq_update for full per-function reference, and lengua’s own docs for
the template syntax, frontmatter model, and storage design that lenguar
is a thin FFI layer over.