| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
|
| 5 |
<head> |
| 6 |
|
| 7 |
<meta charset="utf-8" /> |
| 8 |
<meta name="generator" content="pandoc" /> |
| 9 |
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" /> |
| 10 |
|
| 11 |
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
<title>Template Syntax</title> |
| 16 |
|
| 17 |
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to |
| 18 |
// be compatible with the behavior of Pandoc < 2.8). |
| 19 |
document.addEventListener('DOMContentLoaded', function(e) { |
| 20 |
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); |
| 21 |
var i, h, a; |
| 22 |
for (i = 0; i < hs.length; i++) { |
| 23 |
h = hs[i]; |
| 24 |
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 |
| 25 |
a = h.attributes; |
| 26 |
while (a.length > 0) h.removeAttribute(a[0].name); |
| 27 |
} |
| 28 |
}); |
| 29 |
</script> |
| 30 |
|
| 31 |
<style type="text/css"> |
| 32 |
code{white-space: pre-wrap;} |
| 33 |
span.smallcaps{font-variant: small-caps;} |
| 34 |
span.underline{text-decoration: underline;} |
| 35 |
div.column{display: inline-block; vertical-align: top; width: 50%;} |
| 36 |
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} |
| 37 |
ul.task-list{list-style: none;} |
| 38 |
</style> |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
<style type="text/css"> |
| 43 |
code { |
| 44 |
white-space: pre; |
| 45 |
} |
| 46 |
.sourceCode { |
| 47 |
overflow: visible; |
| 48 |
} |
| 49 |
</style> |
| 50 |
<style type="text/css" data-origin="pandoc"> |
| 51 |
pre > code.sourceCode { white-space: pre; position: relative; } |
| 52 |
pre > code.sourceCode > span { line-height: 1.25; } |
| 53 |
pre > code.sourceCode > span:empty { height: 1.2em; } |
| 54 |
.sourceCode { overflow: visible; } |
| 55 |
code.sourceCode > span { color: inherit; text-decoration: inherit; } |
| 56 |
div.sourceCode { margin: 1em 0; } |
| 57 |
pre.sourceCode { margin: 0; } |
| 58 |
@media screen { |
| 59 |
div.sourceCode { overflow: auto; } |
| 60 |
} |
| 61 |
@media print { |
| 62 |
pre > code.sourceCode { white-space: pre-wrap; } |
| 63 |
pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; } |
| 64 |
} |
| 65 |
pre.numberSource code |
| 66 |
{ counter-reset: source-line 0; } |
| 67 |
pre.numberSource code > span |
| 68 |
{ position: relative; left: -4em; counter-increment: source-line; } |
| 69 |
pre.numberSource code > span > a:first-child::before |
| 70 |
{ content: counter(source-line); |
| 71 |
position: relative; left: -1em; text-align: right; vertical-align: baseline; |
| 72 |
border: none; display: inline-block; |
| 73 |
-webkit-touch-callout: none; -webkit-user-select: none; |
| 74 |
-khtml-user-select: none; -moz-user-select: none; |
| 75 |
-ms-user-select: none; user-select: none; |
| 76 |
padding: 0 4px; width: 4em; |
| 77 |
color: #aaaaaa; |
| 78 |
} |
| 79 |
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; } |
| 80 |
div.sourceCode |
| 81 |
{ } |
| 82 |
@media screen { |
| 83 |
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } |
| 84 |
} |
| 85 |
code span.al { color: #ff0000; font-weight: bold; } |
| 86 |
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 87 |
code span.at { color: #7d9029; } |
| 88 |
code span.bn { color: #40a070; } |
| 89 |
code span.bu { color: #008000; } |
| 90 |
code span.cf { color: #007020; font-weight: bold; } |
| 91 |
code span.ch { color: #4070a0; } |
| 92 |
code span.cn { color: #880000; } |
| 93 |
code span.co { color: #60a0b0; font-style: italic; } |
| 94 |
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 95 |
code span.do { color: #ba2121; font-style: italic; } |
| 96 |
code span.dt { color: #902000; } |
| 97 |
code span.dv { color: #40a070; } |
| 98 |
code span.er { color: #ff0000; font-weight: bold; } |
| 99 |
code span.ex { } |
| 100 |
code span.fl { color: #40a070; } |
| 101 |
code span.fu { color: #06287e; } |
| 102 |
code span.im { color: #008000; font-weight: bold; } |
| 103 |
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 104 |
code span.kw { color: #007020; font-weight: bold; } |
| 105 |
code span.op { color: #666666; } |
| 106 |
code span.ot { color: #007020; } |
| 107 |
code span.pp { color: #bc7a00; } |
| 108 |
code span.sc { color: #4070a0; } |
| 109 |
code span.ss { color: #bb6688; } |
| 110 |
code span.st { color: #4070a0; } |
| 111 |
code span.va { color: #19177c; } |
| 112 |
code span.vs { color: #4070a0; } |
| 113 |
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 114 |
</style> |
| 115 |
<script> |
| 116 |
// apply pandoc div.sourceCode style to pre.sourceCode instead |
| 117 |
(function() { |
| 118 |
var sheets = document.styleSheets; |
| 119 |
for (var i = 0; i < sheets.length; i++) { |
| 120 |
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue; |
| 121 |
try { var rules = sheets[i].cssRules; } catch (e) { continue; } |
| 122 |
var j = 0; |
| 123 |
while (j < rules.length) { |
| 124 |
var rule = rules[j]; |
| 125 |
// check if there is a div.sourceCode rule |
| 126 |
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") { |
| 127 |
j++; |
| 128 |
continue; |
| 129 |
} |
| 130 |
var style = rule.style.cssText; |
| 131 |
// check if color or background-color is set |
| 132 |
if (rule.style.color === '' && rule.style.backgroundColor === '') { |
| 133 |
j++; |
| 134 |
continue; |
| 135 |
} |
| 136 |
// replace div.sourceCode by a pre.sourceCode rule |
| 137 |
sheets[i].deleteRule(j); |
| 138 |
sheets[i].insertRule('pre.sourceCode{' + style + '}', j); |
| 139 |
} |
| 140 |
} |
| 141 |
})(); |
| 142 |
</script> |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
<style type="text/css">body { |
| 148 |
background-color: #fff; |
| 149 |
margin: 1em auto; |
| 150 |
max-width: 700px; |
| 151 |
overflow: visible; |
| 152 |
padding-left: 2em; |
| 153 |
padding-right: 2em; |
| 154 |
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; |
| 155 |
font-size: 14px; |
| 156 |
line-height: 1.35; |
| 157 |
} |
| 158 |
#TOC { |
| 159 |
clear: both; |
| 160 |
margin: 0 0 10px 10px; |
| 161 |
padding: 4px; |
| 162 |
width: 400px; |
| 163 |
border: 1px solid #CCCCCC; |
| 164 |
border-radius: 5px; |
| 165 |
background-color: #f6f6f6; |
| 166 |
font-size: 13px; |
| 167 |
line-height: 1.3; |
| 168 |
} |
| 169 |
#TOC .toctitle { |
| 170 |
font-weight: bold; |
| 171 |
font-size: 15px; |
| 172 |
margin-left: 5px; |
| 173 |
} |
| 174 |
#TOC ul { |
| 175 |
padding-left: 40px; |
| 176 |
margin-left: -1.5em; |
| 177 |
margin-top: 5px; |
| 178 |
margin-bottom: 5px; |
| 179 |
} |
| 180 |
#TOC ul ul { |
| 181 |
margin-left: -2em; |
| 182 |
} |
| 183 |
#TOC li { |
| 184 |
line-height: 16px; |
| 185 |
} |
| 186 |
table { |
| 187 |
margin: 1em auto; |
| 188 |
border-width: 1px; |
| 189 |
border-color: #DDDDDD; |
| 190 |
border-style: outset; |
| 191 |
border-collapse: collapse; |
| 192 |
} |
| 193 |
table th { |
| 194 |
border-width: 2px; |
| 195 |
padding: 5px; |
| 196 |
border-style: inset; |
| 197 |
} |
| 198 |
table td { |
| 199 |
border-width: 1px; |
| 200 |
border-style: inset; |
| 201 |
line-height: 18px; |
| 202 |
padding: 5px 5px; |
| 203 |
} |
| 204 |
table, table th, table td { |
| 205 |
border-left-style: none; |
| 206 |
border-right-style: none; |
| 207 |
} |
| 208 |
table thead, table tr.even { |
| 209 |
background-color: #f7f7f7; |
| 210 |
} |
| 211 |
p { |
| 212 |
margin: 0.5em 0; |
| 213 |
} |
| 214 |
blockquote { |
| 215 |
background-color: #f6f6f6; |
| 216 |
padding: 0.25em 0.75em; |
| 217 |
} |
| 218 |
hr { |
| 219 |
border-style: solid; |
| 220 |
border: none; |
| 221 |
border-top: 1px solid #777; |
| 222 |
margin: 28px 0; |
| 223 |
} |
| 224 |
dl { |
| 225 |
margin-left: 0; |
| 226 |
} |
| 227 |
dl dd { |
| 228 |
margin-bottom: 13px; |
| 229 |
margin-left: 13px; |
| 230 |
} |
| 231 |
dl dt { |
| 232 |
font-weight: bold; |
| 233 |
} |
| 234 |
ul { |
| 235 |
margin-top: 0; |
| 236 |
} |
| 237 |
ul li { |
| 238 |
list-style: circle outside; |
| 239 |
} |
| 240 |
ul ul { |
| 241 |
margin-bottom: 0; |
| 242 |
} |
| 243 |
pre, code { |
| 244 |
background-color: #f7f7f7; |
| 245 |
border-radius: 3px; |
| 246 |
color: #333; |
| 247 |
white-space: pre-wrap; |
| 248 |
} |
| 249 |
pre { |
| 250 |
border-radius: 3px; |
| 251 |
margin: 5px 0px 10px 0px; |
| 252 |
padding: 10px; |
| 253 |
} |
| 254 |
pre:not([class]) { |
| 255 |
background-color: #f7f7f7; |
| 256 |
} |
| 257 |
code { |
| 258 |
font-family: Consolas, Monaco, 'Courier New', monospace; |
| 259 |
font-size: 85%; |
| 260 |
} |
| 261 |
p > code, li > code { |
| 262 |
padding: 2px 0px; |
| 263 |
} |
| 264 |
div.figure { |
| 265 |
text-align: center; |
| 266 |
} |
| 267 |
img { |
| 268 |
background-color: #FFFFFF; |
| 269 |
padding: 2px; |
| 270 |
border: 1px solid #DDDDDD; |
| 271 |
border-radius: 3px; |
| 272 |
border: 1px solid #CCCCCC; |
| 273 |
margin: 0 5px; |
| 274 |
} |
| 275 |
h1 { |
| 276 |
margin-top: 0; |
| 277 |
font-size: 35px; |
| 278 |
line-height: 40px; |
| 279 |
} |
| 280 |
h2 { |
| 281 |
border-bottom: 4px solid #f7f7f7; |
| 282 |
padding-top: 10px; |
| 283 |
padding-bottom: 2px; |
| 284 |
font-size: 145%; |
| 285 |
} |
| 286 |
h3 { |
| 287 |
border-bottom: 2px solid #f7f7f7; |
| 288 |
padding-top: 10px; |
| 289 |
font-size: 120%; |
| 290 |
} |
| 291 |
h4 { |
| 292 |
border-bottom: 1px solid #f7f7f7; |
| 293 |
margin-left: 8px; |
| 294 |
font-size: 105%; |
| 295 |
} |
| 296 |
h5, h6 { |
| 297 |
border-bottom: 1px solid #ccc; |
| 298 |
font-size: 105%; |
| 299 |
} |
| 300 |
a { |
| 301 |
color: #0033dd; |
| 302 |
text-decoration: none; |
| 303 |
} |
| 304 |
a:hover { |
| 305 |
color: #6666ff; } |
| 306 |
a:visited { |
| 307 |
color: #800080; } |
| 308 |
a:visited:hover { |
| 309 |
color: #BB00BB; } |
| 310 |
a[href^="http:"] { |
| 311 |
text-decoration: underline; } |
| 312 |
a[href^="https:"] { |
| 313 |
text-decoration: underline; } |
| 314 |
|
| 315 |
code > span.kw { color: #555; font-weight: bold; } |
| 316 |
code > span.dt { color: #902000; } |
| 317 |
code > span.dv { color: #40a070; } |
| 318 |
code > span.bn { color: #d14; } |
| 319 |
code > span.fl { color: #d14; } |
| 320 |
code > span.ch { color: #d14; } |
| 321 |
code > span.st { color: #d14; } |
| 322 |
code > span.co { color: #888888; font-style: italic; } |
| 323 |
code > span.ot { color: #007020; } |
| 324 |
code > span.al { color: #ff0000; font-weight: bold; } |
| 325 |
code > span.fu { color: #900; font-weight: bold; } |
| 326 |
code > span.er { color: #a61717; background-color: #e3d2d2; } |
| 327 |
</style> |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
</head> |
| 333 |
|
| 334 |
<body> |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
<h1 class="title toc-ignore">Template Syntax</h1> |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
<p>This vignette describes the template syntax supported by jinjar, |
| 344 |
following the structure of the Jinja <a href="https://jinja.palletsprojects.com/templates/">Template Designer |
| 345 |
Documentation</a>. It is designed to act as a reference when writing |
| 346 |
templates.</p> |
| 347 |
<p>The jinjar R package is powered by the <a href="https://github.com/pantor/inja">inja</a> C++ library. The syntax |
| 348 |
is very similar to that of the Jinja Python package, but there are also |
| 349 |
many differences. Unfortunately, this means jinjar is not a drop-in |
| 350 |
replacement for Jinja – you might need to adapt existing Jinja templates |
| 351 |
for the jinjar engine.</p> |
| 352 |
<p>The most fundamental difference between jinjar and Jinja is:</p> |
| 353 |
<ul> |
| 354 |
<li>Jinja variables support direct interaction with the underlying |
| 355 |
Python objects.</li> |
| 356 |
<li>jinjar variables are simple <a href="https://www.w3schools.com/js/js_json_datatypes.asp">JSON data |
| 357 |
types</a>. The underlying R objects are translated to JSON.</li> |
| 358 |
</ul> |
| 359 |
<p>This is described in more detail in the <a href="#variables">Variables</a> section below.</p> |
| 360 |
<p>Before starting, let’s create a few R objects for rendering example |
| 361 |
templates.</p> |
| 362 |
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">library</span>(jinjar)</span> |
| 363 |
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a></span> |
| 364 |
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="co"># length-1 vector</span></span> |
| 365 |
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a>title <span class="ot"><-</span> <span class="st">"My Webpage"</span></span> |
| 366 |
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a></span> |
| 367 |
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="co"># vector</span></span> |
| 368 |
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a>users <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"User A"</span>, <span class="st">"User B"</span>, <span class="st">"User C"</span>)</span> |
| 369 |
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a></span> |
| 370 |
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a><span class="co"># list</span></span> |
| 371 |
<span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a>godzilla <span class="ot"><-</span> <span class="fu">list</span>(</span> |
| 372 |
<span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a> <span class="at">Name =</span> <span class="st">"Godzilla"</span>,</span> |
| 373 |
<span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a> <span class="at">Born =</span> <span class="dv">1952</span>,</span> |
| 374 |
<span id="cb1-13"><a href="#cb1-13" tabindex="-1"></a> <span class="at">Birthplace =</span> <span class="st">"Japan"</span></span> |
| 375 |
<span id="cb1-14"><a href="#cb1-14" tabindex="-1"></a>)</span> |
| 376 |
<span id="cb1-15"><a href="#cb1-15" tabindex="-1"></a></span> |
| 377 |
<span id="cb1-16"><a href="#cb1-16" tabindex="-1"></a><span class="co"># data frame</span></span> |
| 378 |
<span id="cb1-17"><a href="#cb1-17" tabindex="-1"></a>navigation <span class="ot"><-</span> <span class="fu">data.frame</span>(</span> |
| 379 |
<span id="cb1-18"><a href="#cb1-18" tabindex="-1"></a> <span class="at">caption =</span> <span class="fu">c</span>(<span class="st">"Home"</span>, <span class="st">"Blog"</span>),</span> |
| 380 |
<span id="cb1-19"><a href="#cb1-19" tabindex="-1"></a> <span class="at">href =</span> <span class="fu">c</span>(<span class="st">"index.html"</span>, <span class="st">"blog.html"</span>)</span> |
| 381 |
<span id="cb1-20"><a href="#cb1-20" tabindex="-1"></a>)</span> |
| 382 |
<span id="cb1-21"><a href="#cb1-21" tabindex="-1"></a></span> |
| 383 |
<span id="cb1-22"><a href="#cb1-22" tabindex="-1"></a><span class="co"># HTML special characters</span></span> |
| 384 |
<span id="cb1-23"><a href="#cb1-23" tabindex="-1"></a>name <span class="ot"><-</span> <span class="st">'Dwayne "The Rock" Johnson'</span></span></code></pre></div> |
| 385 |
<div id="synopsis" class="section level2"> |
| 386 |
<h2>Synopsis</h2> |
| 387 |
<p>A jinjar template is simply a text file, and when rendered the output |
| 388 |
is also a text file (e.g. HTML, SQL, LaTeX).</p> |
| 389 |
<p>A template contains <strong>variables</strong> and/or |
| 390 |
<strong>expressions</strong>, which get replaced with values when a |
| 391 |
template is rendered; and <strong>tags</strong>, which control the logic |
| 392 |
of the template.</p> |
| 393 |
<p>Below is a minimal template that illustrates a few basics using the |
| 394 |
default jinjar configuration. We will cover the details later in this |
| 395 |
document:</p> |
| 396 |
<div class="sourceCode" id="cb2"><pre class="sourceCode jinjar html bg-info"><code class="sourceCode html"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="dt"><!DOCTYPE</span> html<span class="dt">></span></span> |
| 397 |
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="dt"><</span><span class="kw">html</span><span class="ot"> lang</span><span class="op">=</span><span class="st">"en"</span><span class="dt">></span></span> |
| 398 |
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="dt"><</span><span class="kw">head</span><span class="dt">></span></span> |
| 399 |
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a> <span class="dt"><</span><span class="kw">title</span><span class="dt">></span>{{ title }}<span class="dt"></</span><span class="kw">title</span><span class="dt">></span></span> |
| 400 |
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a><span class="dt"></</span><span class="kw">head</span><span class="dt">></span></span> |
| 401 |
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a><span class="dt"><</span><span class="kw">body</span><span class="dt">></span></span> |
| 402 |
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a> <span class="dt"><</span><span class="kw">ul</span><span class="ot"> id</span><span class="op">=</span><span class="st">"navigation"</span><span class="dt">></span></span> |
| 403 |
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a> {% for item in navigation -%}</span> |
| 404 |
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a> <span class="dt"><</span><span class="kw">li</span><span class="dt">><</span><span class="kw">a</span><span class="ot"> href</span><span class="op">=</span><span class="st">"{{ item.href }}"</span><span class="dt">></span>{{ item.caption }}<span class="dt"></</span><span class="kw">a</span><span class="dt">></</span><span class="kw">li</span><span class="dt">></span></span> |
| 405 |
<span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a> {% endfor -%}</span> |
| 406 |
<span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a> <span class="dt"></</span><span class="kw">ul</span><span class="dt">></span></span> |
| 407 |
<span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a> {# a comment #}</span> |
| 408 |
<span id="cb2-13"><a href="#cb2-13" tabindex="-1"></a><span class="dt"></</span><span class="kw">body</span><span class="dt">></span></span> |
| 409 |
<span id="cb2-14"><a href="#cb2-14" tabindex="-1"></a><span class="dt"></</span><span class="kw">html</span><span class="dt">></span></span></code></pre></div> |
| 410 |
<div class="sourceCode" id="cb3"><pre class="sourceCode html bg-success"><code class="sourceCode html"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="dt"><!DOCTYPE</span> html<span class="dt">></span></span> |
| 411 |
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="dt"><</span><span class="kw">html</span><span class="ot"> lang</span><span class="op">=</span><span class="st">"en"</span><span class="dt">></span></span> |
| 412 |
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="dt"><</span><span class="kw">head</span><span class="dt">></span></span> |
| 413 |
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a> <span class="dt"><</span><span class="kw">title</span><span class="dt">></span>My Webpage<span class="dt"></</span><span class="kw">title</span><span class="dt">></span></span> |
| 414 |
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="dt"></</span><span class="kw">head</span><span class="dt">></span></span> |
| 415 |
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="dt"><</span><span class="kw">body</span><span class="dt">></span></span> |
| 416 |
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a> <span class="dt"><</span><span class="kw">ul</span><span class="ot"> id</span><span class="op">=</span><span class="st">"navigation"</span><span class="dt">></span></span> |
| 417 |
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a> <span class="dt"><</span><span class="kw">li</span><span class="dt">><</span><span class="kw">a</span><span class="ot"> href</span><span class="op">=</span><span class="st">"index.html"</span><span class="dt">></span>Home<span class="dt"></</span><span class="kw">a</span><span class="dt">></</span><span class="kw">li</span><span class="dt">></span></span> |
| 418 |
<span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a> <span class="dt"><</span><span class="kw">li</span><span class="dt">><</span><span class="kw">a</span><span class="ot"> href</span><span class="op">=</span><span class="st">"blog.html"</span><span class="dt">></span>Blog<span class="dt"></</span><span class="kw">a</span><span class="dt">></</span><span class="kw">li</span><span class="dt">></span></span> |
| 419 |
<span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a> <span class="dt"></</span><span class="kw">ul</span><span class="dt">></span></span> |
| 420 |
<span id="cb3-11"><a href="#cb3-11" tabindex="-1"></a> </span> |
| 421 |
<span id="cb3-12"><a href="#cb3-12" tabindex="-1"></a><span class="dt"></</span><span class="kw">body</span><span class="dt">></span></span> |
| 422 |
<span id="cb3-13"><a href="#cb3-13" tabindex="-1"></a><span class="dt"></</span><span class="kw">html</span><span class="dt">></span></span></code></pre></div> |
| 423 |
<p>The following example shows the default configuration settings, but |
| 424 |
you can adjust the syntax configuration as desired using |
| 425 |
<code>jinjar_config()</code>.</p> |
| 426 |
<p>There are a few kinds of delimiters. The default delimiters are |
| 427 |
configured as follows:</p> |
| 428 |
<ul> |
| 429 |
<li><code>{% ... %}</code> for <a href="#control-structures">Statements</a></li> |
| 430 |
<li><code>{{ ... }}</code> for <a href="#expressions">Expressions</a> to |
| 431 |
print to the template output</li> |
| 432 |
<li><code>{# ... #}</code> for <a href="#comments">Comments</a> not |
| 433 |
included in the template output</li> |
| 434 |
</ul> |
| 435 |
<p><a href="#line-statements">Line Statements</a> are also possible, |
| 436 |
though they don’t have default prefix characters. To use them, set |
| 437 |
<code>line_statement</code> when creating the |
| 438 |
<code>jinjar_config()</code>.</p> |
| 439 |
</div> |
| 440 |
<div id="variables" class="section level2"> |
| 441 |
<h2>Variables</h2> |
| 442 |
<p>When writing a template, we refer to variables that act as data |
| 443 |
placeholders. We define their values when rendering the template.</p> |
| 444 |
<p>Although we pass R objects to <code>render()</code>, it is helpful to |
| 445 |
understand that these are encoded as JSON objects before the template is |
| 446 |
rendered.</p> |
| 447 |
<table> |
| 448 |
<thead> |
| 449 |
<tr class="header"> |
| 450 |
<th align="left">R object</th> |
| 451 |
<th align="left">JSON object</th> |
| 452 |
<th align="left">Template example</th> |
| 453 |
</tr> |
| 454 |
</thead> |
| 455 |
<tbody> |
| 456 |
<tr class="odd"> |
| 457 |
<td align="left">Length-1 vector</td> |
| 458 |
<td align="left">Scalar</td> |
| 459 |
<td align="left"><code>{{ foo }}</code></td> |
| 460 |
</tr> |
| 461 |
<tr class="even"> |
| 462 |
<td align="left">Vector</td> |
| 463 |
<td align="left">Array</td> |
| 464 |
<td align="left"><code>{{ foo.1 }}</code></td> |
| 465 |
</tr> |
| 466 |
<tr class="odd"> |
| 467 |
<td align="left">List</td> |
| 468 |
<td align="left">Object</td> |
| 469 |
<td align="left"><code>{{ foo.bar }}</code></td> |
| 470 |
</tr> |
| 471 |
<tr class="even"> |
| 472 |
<td align="left">Data frame</td> |
| 473 |
<td align="left">Array of objects</td> |
| 474 |
<td align="left"><code>{{ foo.1.bar }}</code></td> |
| 475 |
</tr> |
| 476 |
</tbody> |
| 477 |
</table> |
| 478 |
<p>You can use dot (<code>.</code>) notation to access data nested |
| 479 |
within a variable. An array element is accessed by its numeric |
| 480 |
<strong>zero-based</strong> index (e.g. <code>foo.1</code>) and an |
| 481 |
object value is accessed by its key (e.g. <code>foo.bar</code>).</p> |
| 482 |
<p><strong>Note:</strong> In R, the dot is a valid character in an |
| 483 |
object name (e.g. <code>my.data</code>). However, this causes ambiguity |
| 484 |
when accessing nested data values. For this reason, each dot is replaced |
| 485 |
with an underscore when the data is encoded as JSON |
| 486 |
(e.g. <code>my.data</code> becomes <code>my_data</code>).</p> |
| 487 |
<p><strong>Note:</strong> In R, a scalar is indistinguishable from a |
| 488 |
length-1 vector. This creates an ambiguity when passing R data to the |
| 489 |
template, because template variables support both scalars and arrays. |
| 490 |
You can explicitly pass a length-1 vector as an array using the |
| 491 |
<code>I()</code> operator (see <code>help("render")</code>).</p> |
| 492 |
<p>The double-brace syntax is used to print the value of the variable |
| 493 |
(e.g. <code>{{ foo }}</code>). To use the variable in other contexts |
| 494 |
(e.g. control structures), then these braces are omitted |
| 495 |
(e.g. <code>{% for bar in foo %}</code>).</p> |
| 496 |
<p>If a template variable has not been defined, then an error occurs. |
| 497 |
However, you can use the <code>default(foo, bar)</code> function to |
| 498 |
specify a fallback value.</p> |
| 499 |
</div> |
| 500 |
<div id="comments" class="section level2"> |
| 501 |
<h2>Comments</h2> |
| 502 |
<p>To comment-out some lines, preventing them from appearing in the |
| 503 |
rendered document, use the comment syntax (default: |
| 504 |
<code>{# ... #}</code>). This is useful for debugging or documenting the |
| 505 |
template.</p> |
| 506 |
<pre class="jinjar bg-info"><code>Hello{# TODO: update this #}!</code></pre> |
| 507 |
<pre class="bg-success"><code>Hello!</code></pre> |
| 508 |
</div> |
| 509 |
<div id="whitespace" class="section level2"> |
| 510 |
<h2>Whitespace Control</h2> |
| 511 |
<p>In the default configuration, whitespace (e.g. spaces, tabs, |
| 512 |
newlines) is left unchanged in the rendered output. For example, in the |
| 513 |
default configuration we get:</p> |
| 514 |
<div class="sourceCode" id="cb6"><pre class="sourceCode jinjar html bg-info"><code class="sourceCode html"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">div</span><span class="dt">></span></span> |
| 515 |
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a> {% if true %}</span> |
| 516 |
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a> yay</span> |
| 517 |
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a> {% endif %}</span> |
| 518 |
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a><span class="dt"></</span><span class="kw">div</span><span class="dt">></span></span></code></pre></div> |
| 519 |
<div class="sourceCode" id="cb7"><pre class="sourceCode html bg-success"><code class="sourceCode html"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">div</span><span class="dt">></span></span> |
| 520 |
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a> </span> |
| 521 |
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a> yay</span> |
| 522 |
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a> </span> |
| 523 |
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="dt"></</span><span class="kw">div</span><span class="dt">></span></span></code></pre></div> |
| 524 |
<p>By setting <code>trim_blocks = TRUE</code> when creating the |
| 525 |
<code>jinjar_config()</code>, the first newline after a control block is |
| 526 |
automatically removed. Setting <code>lstrip_blocks = TRUE</code> removes |
| 527 |
any whitespace from the beginning of the line until the start of each |
| 528 |
block. With both options enabled, the above example becomes:</p> |
| 529 |
<div class="sourceCode" id="cb8"><pre class="sourceCode jinjar html bg-info"><code class="sourceCode html"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">div</span><span class="dt">></span></span> |
| 530 |
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a> {% if true %}</span> |
| 531 |
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a> yay</span> |
| 532 |
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a> {% endif %}</span> |
| 533 |
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a><span class="dt"></</span><span class="kw">div</span><span class="dt">></span></span></code></pre></div> |
| 534 |
<div class="sourceCode" id="cb9"><pre class="sourceCode html bg-success"><code class="sourceCode html"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">div</span><span class="dt">></span></span> |
| 535 |
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a> yay</span> |
| 536 |
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a><span class="dt"></</span><span class="kw">div</span><span class="dt">></span></span></code></pre></div> |
| 537 |
<p>Instead of changing the global configuration, you can manually trim |
| 538 |
whitespace at a more finegrained level.</p> |
| 539 |
<ul> |
| 540 |
<li>By putting a minus sign (<code>-</code>) after the opening |
| 541 |
delimiter, this removes any whitespace from the beginning of the line |
| 542 |
until the start of the block (i.e. the same as the |
| 543 |
<code>lstrip_blocks</code> feature).</li> |
| 544 |
<li>By putting a minus sign (<code>-</code>) before the closing |
| 545 |
delimiter, this removes any whitespace (including newlines) until the |
| 546 |
next non-whitespace character (i.e. slightly different from the |
| 547 |
<code>trim_blocks</code> feature).</li> |
| 548 |
</ul> |
| 549 |
<p>This can be activated for control blocks, comments, or variable |
| 550 |
expressions:</p> |
| 551 |
<div class="sourceCode" id="cb10"><pre class="sourceCode jinjar html bg-info"><code class="sourceCode html"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">div</span><span class="dt">></span></span> |
| 552 |
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a> {% if true -%}</span> |
| 553 |
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a> yay</span> |
| 554 |
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a> {%- endif -%}</span> |
| 555 |
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a><span class="dt"></</span><span class="kw">div</span><span class="dt">></span></span></code></pre></div> |
| 556 |
<div class="sourceCode" id="cb11"><pre class="sourceCode html bg-success"><code class="sourceCode html"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">div</span><span class="dt">></span></span> |
| 557 |
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a> yay</span> |
| 558 |
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="dt"></</span><span class="kw">div</span><span class="dt">></span></span></code></pre></div> |
| 559 |
</div> |
| 560 |
<div id="line-statements" class="section level2"> |
| 561 |
<h2>Line Statements</h2> |
| 562 |
<p>If line statements are enabled (see <code>jinjar_config()</code>), |
| 563 |
it’s possible to mark a line as a statement. For example, if the line |
| 564 |
statement prefix is configured to <code>#</code>, you can do:</p> |
| 565 |
<div class="sourceCode" id="cb12"><pre class="sourceCode jinjar html bg-info"><code class="sourceCode html"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">ul</span><span class="ot"> id</span><span class="op">=</span><span class="st">"navigation"</span><span class="dt">></span></span> |
| 566 |
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a># for item in navigation</span> |
| 567 |
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a> <span class="dt"><</span><span class="kw">li</span><span class="dt">><</span><span class="kw">a</span><span class="ot"> href</span><span class="op">=</span><span class="st">"{{ item.href }}"</span><span class="dt">></span>{{ item.caption }}<span class="dt"></</span><span class="kw">a</span><span class="dt">></</span><span class="kw">li</span><span class="dt">></span></span> |
| 568 |
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a># endfor</span> |
| 569 |
<span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a><span class="dt"></</span><span class="kw">ul</span><span class="dt">></span></span></code></pre></div> |
| 570 |
<div class="sourceCode" id="cb13"><pre class="sourceCode html bg-success"><code class="sourceCode html"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">ul</span><span class="ot"> id</span><span class="op">=</span><span class="st">"navigation"</span><span class="dt">></span></span> |
| 571 |
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a> <span class="dt"><</span><span class="kw">li</span><span class="dt">><</span><span class="kw">a</span><span class="ot"> href</span><span class="op">=</span><span class="st">"index.html"</span><span class="dt">></span>Home<span class="dt"></</span><span class="kw">a</span><span class="dt">></</span><span class="kw">li</span><span class="dt">></span></span> |
| 572 |
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a> <span class="dt"><</span><span class="kw">li</span><span class="dt">><</span><span class="kw">a</span><span class="ot"> href</span><span class="op">=</span><span class="st">"blog.html"</span><span class="dt">></span>Blog<span class="dt"></</span><span class="kw">a</span><span class="dt">></</span><span class="kw">li</span><span class="dt">></span></span> |
| 573 |
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a><span class="dt"></</span><span class="kw">ul</span><span class="dt">></span></span></code></pre></div> |
| 574 |
</div> |
| 575 |
<div id="control-structures" class="section level2"> |
| 576 |
<h2>Control Structures</h2> |
| 577 |
<p>A control structure refers to all those things that control the flow |
| 578 |
of a program. With the default syntax, control structures appear inside |
| 579 |
<code>{% ... %}</code> blocks.</p> |
| 580 |
<div id="for" class="section level3"> |
| 581 |
<h3>For</h3> |
| 582 |
<p>A for-loop allows you to iterate over each element in a vector:</p> |
| 583 |
<div class="sourceCode" id="cb14"><pre class="sourceCode jinjar markdown bg-info"><code class="sourceCode markdown"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a>{% for user in users -%}</span> |
| 584 |
<span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a>{{ loop.index1 }}. {{ user }}</span> |
| 585 |
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a>{%- endfor -%}</span></code></pre></div> |
| 586 |
<div class="sourceCode" id="cb15"><pre class="sourceCode markdown bg-success"><code class="sourceCode markdown"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a><span class="ss">1. </span>User A</span> |
| 587 |
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a><span class="ss">2. </span>User B</span> |
| 588 |
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a><span class="ss">3. </span>User C</span></code></pre></div> |
| 589 |
<p>or loop over key-value pairs in a named list:</p> |
| 590 |
<div class="sourceCode" id="cb16"><pre class="sourceCode jinjar html bg-info"><code class="sourceCode html"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">dl</span><span class="dt">></span></span> |
| 591 |
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a>{% for key, value in godzilla %}</span> |
| 592 |
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a> <span class="dt"><</span><span class="kw">dt</span><span class="dt">></span>{{ key }}<span class="dt"></</span><span class="kw">dt</span><span class="dt">></span></span> |
| 593 |
<span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a> <span class="dt"><</span><span class="kw">dd</span><span class="dt">></span>{{ value }}<span class="dt"></</span><span class="kw">dd</span><span class="dt">></span></span> |
| 594 |
<span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a>{% endfor -%}</span> |
| 595 |
<span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a><span class="dt"></</span><span class="kw">dl</span><span class="dt">></span></span></code></pre></div> |
| 596 |
<div class="sourceCode" id="cb17"><pre class="sourceCode html bg-success"><code class="sourceCode html"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">dl</span><span class="dt">></span></span> |
| 597 |
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a></span> |
| 598 |
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a> <span class="dt"><</span><span class="kw">dt</span><span class="dt">></span>Birthplace<span class="dt"></</span><span class="kw">dt</span><span class="dt">></span></span> |
| 599 |
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a> <span class="dt"><</span><span class="kw">dd</span><span class="dt">></span>Japan<span class="dt"></</span><span class="kw">dd</span><span class="dt">></span></span> |
| 600 |
<span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a></span> |
| 601 |
<span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a> <span class="dt"><</span><span class="kw">dt</span><span class="dt">></span>Born<span class="dt"></</span><span class="kw">dt</span><span class="dt">></span></span> |
| 602 |
<span id="cb17-7"><a href="#cb17-7" tabindex="-1"></a> <span class="dt"><</span><span class="kw">dd</span><span class="dt">></span>1952<span class="dt"></</span><span class="kw">dd</span><span class="dt">></span></span> |
| 603 |
<span id="cb17-8"><a href="#cb17-8" tabindex="-1"></a></span> |
| 604 |
<span id="cb17-9"><a href="#cb17-9" tabindex="-1"></a> <span class="dt"><</span><span class="kw">dt</span><span class="dt">></span>Name<span class="dt"></</span><span class="kw">dt</span><span class="dt">></span></span> |
| 605 |
<span id="cb17-10"><a href="#cb17-10" tabindex="-1"></a> <span class="dt"><</span><span class="kw">dd</span><span class="dt">></span>Godzilla<span class="dt"></</span><span class="kw">dd</span><span class="dt">></span></span> |
| 606 |
<span id="cb17-11"><a href="#cb17-11" tabindex="-1"></a><span class="dt"></</span><span class="kw">dl</span><span class="dt">></span></span></code></pre></div> |
| 607 |
<p>As described in <a href="#variables">Variables</a>, a data frame is |
| 608 |
translated to an array of JSON objects. Therefore a nested combination |
| 609 |
of the above two loops could theoretically be used. In practice, it is |
| 610 |
much more common to iterate over rows and access the individual elements |
| 611 |
by their attributes:</p> |
| 612 |
<div class="sourceCode" id="cb18"><pre class="sourceCode jinjar html bg-info"><code class="sourceCode html"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">ul</span><span class="ot"> id</span><span class="op">=</span><span class="st">"navigation"</span><span class="dt">></span></span> |
| 613 |
<span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a>{% for item in navigation -%}</span> |
| 614 |
<span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a><span class="dt"><</span><span class="kw">li</span><span class="dt">><</span><span class="kw">a</span><span class="ot"> href</span><span class="op">=</span><span class="st">"{{ item.href }}"</span><span class="dt">></span>{{ item.caption }}<span class="dt"></</span><span class="kw">a</span><span class="dt">></</span><span class="kw">li</span><span class="dt">></span></span> |
| 615 |
<span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a>{% endfor -%}</span> |
| 616 |
<span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a><span class="dt"></</span><span class="kw">ul</span><span class="dt">></span></span></code></pre></div> |
| 617 |
<div class="sourceCode" id="cb19"><pre class="sourceCode html bg-success"><code class="sourceCode html"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">ul</span><span class="ot"> id</span><span class="op">=</span><span class="st">"navigation"</span><span class="dt">></span></span> |
| 618 |
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a><span class="dt"><</span><span class="kw">li</span><span class="dt">><</span><span class="kw">a</span><span class="ot"> href</span><span class="op">=</span><span class="st">"index.html"</span><span class="dt">></span>Home<span class="dt"></</span><span class="kw">a</span><span class="dt">></</span><span class="kw">li</span><span class="dt">></span></span> |
| 619 |
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a><span class="dt"><</span><span class="kw">li</span><span class="dt">><</span><span class="kw">a</span><span class="ot"> href</span><span class="op">=</span><span class="st">"blog.html"</span><span class="dt">></span>Blog<span class="dt"></</span><span class="kw">a</span><span class="dt">></</span><span class="kw">li</span><span class="dt">></span></span> |
| 620 |
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a><span class="dt"></</span><span class="kw">ul</span><span class="dt">></span></span></code></pre></div> |
| 621 |
<p>While inside a for-loop block, you can access some special |
| 622 |
variables:</p> |
| 623 |
<table> |
| 624 |
<thead> |
| 625 |
<tr class="header"> |
| 626 |
<th align="left">Variable</th> |
| 627 |
<th align="left">Description</th> |
| 628 |
</tr> |
| 629 |
</thead> |
| 630 |
<tbody> |
| 631 |
<tr class="odd"> |
| 632 |
<td align="left"><code>loop.index</code></td> |
| 633 |
<td align="left">The current iteration (0-based).</td> |
| 634 |
</tr> |
| 635 |
<tr class="even"> |
| 636 |
<td align="left"><code>loop.index1</code></td> |
| 637 |
<td align="left">The current iteration (1-based).</td> |
| 638 |
</tr> |
| 639 |
<tr class="odd"> |
| 640 |
<td align="left"><code>loop.is_first</code></td> |
| 641 |
<td align="left">True if first iteration.</td> |
| 642 |
</tr> |
| 643 |
<tr class="even"> |
| 644 |
<td align="left"><code>loop.is_last</code></td> |
| 645 |
<td align="left">True if last iteration.</td> |
| 646 |
</tr> |
| 647 |
<tr class="odd"> |
| 648 |
<td align="left"><code>loop.parent</code></td> |
| 649 |
<td align="left">In nested loops, the parent loop variable.</td> |
| 650 |
</tr> |
| 651 |
</tbody> |
| 652 |
</table> |
| 653 |
</div> |
| 654 |
<div id="if" class="section level3"> |
| 655 |
<h3>If</h3> |
| 656 |
<p>Conditional branches are written using <code>if</code>, |
| 657 |
<code>else if</code> and <code>else</code> statements, which evaluate <a href="#expressions">Expressions</a>.</p> |
| 658 |
<div class="sourceCode" id="cb20"><pre class="sourceCode jinjar markdown bg-info"><code class="sourceCode markdown"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a>{% if length(users) > 5 -%}</span> |
| 659 |
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a>{% for user in users -%}</span> |
| 660 |
<span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a><span class="ss">* </span>{{ user }}</span> |
| 661 |
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a>{% endfor %}</span> |
| 662 |
<span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a>{% else if length(users) > 0 -%}</span> |
| 663 |
<span id="cb20-6"><a href="#cb20-6" tabindex="-1"></a>{{ join(users, ", ") }}.</span> |
| 664 |
<span id="cb20-7"><a href="#cb20-7" tabindex="-1"></a>{% else -%}</span> |
| 665 |
<span id="cb20-8"><a href="#cb20-8" tabindex="-1"></a>No users found.</span> |
| 666 |
<span id="cb20-9"><a href="#cb20-9" tabindex="-1"></a>{% endif %}</span></code></pre></div> |
| 667 |
<div class="sourceCode" id="cb21"><pre class="sourceCode markdown bg-success"><code class="sourceCode markdown"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a>User A, User B, User C.</span></code></pre></div> |
| 668 |
</div> |
| 669 |
<div id="assignments" class="section level3"> |
| 670 |
<h3>Assignments</h3> |
| 671 |
<p>Using the <code>set</code> statement, you can assign values to |
| 672 |
variables.</p> |
| 673 |
<pre class="jinjar bg-info"><code>{% set name="world" -%} |
| 674 |
Hello {{ name }}!</code></pre> |
| 675 |
<pre class="bg-success"><code>Hello world!</code></pre> |
| 676 |
</div> |
| 677 |
<div id="extends" class="section level3"> |
| 678 |
<h3>Extends</h3> |
| 679 |
<p>The <code>extends</code> tag can be used for template inheritance. |
| 680 |
See <em>Template Inheritance</em> in |
| 681 |
<code>vignette("auxiliary-templates")</code>.</p> |
| 682 |
</div> |
| 683 |
<div id="include" class="section level3"> |
| 684 |
<h3>Include</h3> |
| 685 |
<p>The <code>include</code> tag inserts the rendered contents of an |
| 686 |
auxiliary template. See <em>Template Inclusion</em> in |
| 687 |
<code>vignette("auxiliary-templates")</code>.</p> |
| 688 |
</div> |
| 689 |
</div> |
| 690 |
<div id="expressions" class="section level2"> |
| 691 |
<h2>Expressions</h2> |
| 692 |
<p>Basic expressions are supported in templates.</p> |
| 693 |
<div id="literals" class="section level3"> |
| 694 |
<h3>Literals</h3> |
| 695 |
<p>The simplest form of expressions are literals, which represent fixed |
| 696 |
values.</p> |
| 697 |
<p>As described in <a href="#variables">Variables</a>, the template is |
| 698 |
rendered using data stored in JSON format. For this reason, literals |
| 699 |
must also be specified in <a href="https://www.w3schools.com/js/js_json_datatypes.asp">JSON |
| 700 |
format</a>. The following types of literals are supported:</p> |
| 701 |
<ul> |
| 702 |
<li><strong>String:</strong> characters between double quotation marks. |
| 703 |
<ul> |
| 704 |
<li>Double quotation marks in the string value must be escaped using a |
| 705 |
backslash.</li> |
| 706 |
</ul></li> |
| 707 |
<li><strong>Integer:</strong> whole numbers without decimal part.</li> |
| 708 |
<li><strong>Numeric:</strong> floating point numbers. |
| 709 |
<ul> |
| 710 |
<li>Specify in decimal or scientific format.</li> |
| 711 |
</ul></li> |
| 712 |
<li><strong>Boolean:</strong> either <code>true</code> or |
| 713 |
<code>false</code>. |
| 714 |
<ul> |
| 715 |
<li>Specify using lowercase characters.</li> |
| 716 |
</ul></li> |
| 717 |
<li><strong>List:</strong> array of values between square brackets.</li> |
| 718 |
<li><strong>Object:</strong> key-value data pairs between curly brackets |
| 719 |
<ul> |
| 720 |
<li>Keys must be string literals, but values can be any literal |
| 721 |
type.</li> |
| 722 |
</ul></li> |
| 723 |
<li><strong>NULL:</strong> missing data is represented by |
| 724 |
<code>null</code>.</li> |
| 725 |
</ul> |
| 726 |
<p>Here is example usage for each type:</p> |
| 727 |
<pre class="jinjar bg-info"><code>String: {{ "A string" }} |
| 728 |
Integer: {{ 3 }} |
| 729 |
Numeric: {{ 3.14 }} or {{ 1.6e-19 }} |
| 730 |
Boolean: {{ true }} or {{ false }} |
| 731 |
List: {{ [1, 2, 3] }} |
| 732 |
Object: {{ {"a": 1, "b": 2} }} |
| 733 |
Null: {{ null }}</code></pre> |
| 734 |
<pre class="bg-success"><code>String: A string |
| 735 |
Integer: 3 |
| 736 |
Numeric: 3.14 or 1.6e-19 |
| 737 |
Boolean: true or false |
| 738 |
List: [1,2,3] |
| 739 |
Object: {"a":1,"b":2} |
| 740 |
Null: </code></pre> |
| 741 |
</div> |
| 742 |
<div id="math" class="section level3"> |
| 743 |
<h3>Math</h3> |
| 744 |
<p>You can perform simple arithmetic using standard operators:</p> |
| 745 |
<pre class="jinjar bg-info"><code>1 + 1: {{ 1 + 1 }} |
| 746 |
3 - 2: {{ 3 - 2 }} |
| 747 |
2 * 2: {{ 2 * 2 }} |
| 748 |
1 / 2: {{ 1 / 2 }} |
| 749 |
2 ^ 3: {{ 2 ^ 3 }} |
| 750 |
7 % 3: {{ 7 % 3 }}</code></pre> |
| 751 |
<pre class="bg-success"><code>1 + 1: 2 |
| 752 |
3 - 2: 1 |
| 753 |
2 * 2: 4 |
| 754 |
1 / 2: 0.5 |
| 755 |
2 ^ 3: 8 |
| 756 |
7 % 3: 1</code></pre> |
| 757 |
</div> |
| 758 |
<div id="comparisons" class="section level3"> |
| 759 |
<h3>Comparisons</h3> |
| 760 |
<p>You can perform comparisons:</p> |
| 761 |
<pre class="jinjar bg-info"><code>1 == 1: {{ 1 == 1 }} |
| 762 |
1 != 1: {{ 1 != 1 }} |
| 763 |
2 > 1: {{ 2 > 1 }} |
| 764 |
2 >= 1: {{ 2 >= 1 }} |
| 765 |
2 < 1: {{ 2 < 1 }} |
| 766 |
2 <= 1: {{ 2 <= 1 }}</code></pre> |
| 767 |
<pre class="bg-success"><code>1 == 1: true |
| 768 |
1 != 1: false |
| 769 |
2 > 1: true |
| 770 |
2 >= 1: true |
| 771 |
2 < 1: false |
| 772 |
2 <= 1: false</code></pre> |
| 773 |
</div> |
| 774 |
<div id="logic" class="section level3"> |
| 775 |
<h3>Logic</h3> |
| 776 |
<p>Within expressions and control structures, you can use the Boolean |
| 777 |
operators: <code>and</code>, <code>or</code>, and <code>not</code>.</p> |
| 778 |
<pre class="jinjar bg-info"><code>true and false: {{ true and false }} |
| 779 |
true or false: {{ true or false }} |
| 780 |
not false: {{ not false }}</code></pre> |
| 781 |
<pre class="bg-success"><code>true and false: false |
| 782 |
true or false: true |
| 783 |
not false: true</code></pre> |
| 784 |
<p>You can also check if a value is contained within a list using |
| 785 |
<code>in</code>:</p> |
| 786 |
<pre class="jinjar bg-info"><code>{{ 1 in [1, 2, 3] }}</code></pre> |
| 787 |
<pre class="bg-success"><code>true</code></pre> |
| 788 |
</div> |
| 789 |
</div> |
| 790 |
<div id="functions" class="section level2"> |
| 791 |
<h2>Functions</h2> |
| 792 |
<div id="data-checks" class="section level3"> |
| 793 |
<h3>Data Checks</h3> |
| 794 |
<p>You can check if a value exists by passing the variable name as a |
| 795 |
string:</p> |
| 796 |
<pre class="jinjar bg-info"><code>users does exist: {{ exists("users") }} |
| 797 |
abc doesn't exist: {{ exists("abc") }}</code></pre> |
| 798 |
<pre class="bg-success"><code>users does exist: true |
| 799 |
abc doesn't exist: false</code></pre> |
| 800 |
<p>Similarly, you can check if a value exists within a JSON object, by |
| 801 |
passing the key as a string:</p> |
| 802 |
<pre class="jinjar bg-info"><code>Birthplace does exist: {{ existsIn(godzilla, "Birthplace") }} |
| 803 |
Weight doesn't exist: {{ existsIn(godzilla, "Weight") }}</code></pre> |
| 804 |
<pre class="bg-success"><code>Birthplace does exist: true |
| 805 |
Weight doesn't exist: false</code></pre> |
| 806 |
<p>Concisely handle missing values using the <code>default()</code> |
| 807 |
function:</p> |
| 808 |
<pre class="jinjar bg-info"><code>{{ default(godzilla.Weight, 20000) }}</code></pre> |
| 809 |
<pre class="bg-success"><code>20000</code></pre> |
| 810 |
<p>You can also check the data type of a variable or literal:</p> |
| 811 |
<pre class="jinjar bg-info"><code>{{ isString("a string") }} |
| 812 |
{{ isInteger(3) }} |
| 813 |
{{ isFloat(3.14) }} |
| 814 |
{{ isNumber(3) }} and {{ isNumber(3.14) }} |
| 815 |
{{ isBoolean(false) }} |
| 816 |
{{ isArray([1, 2, 3]) }} |
| 817 |
{{ isObject({"a": 1, "b": 2}) }}</code></pre> |
| 818 |
<pre class="bg-success"><code>true |
| 819 |
true |
| 820 |
true |
| 821 |
true and true |
| 822 |
true |
| 823 |
true |
| 824 |
true</code></pre> |
| 825 |
</div> |
| 826 |
<div id="data-conversion" class="section level3"> |
| 827 |
<h3>Data Conversion</h3> |
| 828 |
<p>You can convert strings to numeric types, using the |
| 829 |
<code>int()</code> or <code>float()</code> functions:</p> |
| 830 |
<pre class="jinjar bg-info"><code>{{ int("2") }} |
| 831 |
{{ float("2.5") }}</code></pre> |
| 832 |
<pre class="bg-success"><code>2 |
| 833 |
2.5</code></pre> |
| 834 |
</div> |
| 835 |
<div id="html-escaping" class="section level3"> |
| 836 |
<h3>HTML Escaping</h3> |
| 837 |
<p>When generating HTML from templates, there’s always a risk that a |
| 838 |
variable will include characters that affect the resulting HTML. The |
| 839 |
special characters are: <code><</code>, <code>></code>, |
| 840 |
<code>&</code> and <code>"</code>.</p> |
| 841 |
<p>In jinjar, it’s <strong>your</strong> responsibility to manually |
| 842 |
escape variables, using the <code>escape_html()</code> function. You |
| 843 |
should escape variables that <em>might</em> contain any of the special |
| 844 |
characters. But if a variable is trusted to contain well-formed HTML, |
| 845 |
then it should not be escaped (otherwise you could accidentally |
| 846 |
double-escape the content).</p> |
| 847 |
<div class="sourceCode" id="cb44"><pre class="sourceCode jinjar html bg-info"><code class="sourceCode html"><span id="cb44-1"><a href="#cb44-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">input</span><span class="ot"> type</span><span class="op">=</span><span class="st">"text"</span><span class="ot"> value</span><span class="op">=</span><span class="st">"{{ escape_html(name) }}"</span><span class="dt">></span></span></code></pre></div> |
| 848 |
<div class="sourceCode" id="cb45"><pre class="sourceCode html bg-success"><code class="sourceCode html"><span id="cb45-1"><a href="#cb45-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">input</span><span class="ot"> type</span><span class="op">=</span><span class="st">"text"</span><span class="ot"> value</span><span class="op">=</span><span class="st">"Dwayne </span><span class="dv">&quot;</span><span class="st">The Rock</span><span class="dv">&quot;</span><span class="st"> Johnson"</span><span class="dt">></span></span></code></pre></div> |
| 849 |
</div> |
| 850 |
<div id="sql-quoting" class="section level3"> |
| 851 |
<h3>SQL Quoting</h3> |
| 852 |
<p>SQL databases expect string literals to be wrapped in single-quotes, |
| 853 |
while other types of literals (e.g., numbers) are not quoted. This is |
| 854 |
cumbersome to achieve when writing a template, so the |
| 855 |
<code>quote_sql()</code> function provides this functionality.</p> |
| 856 |
<p><strong>Important:</strong> <code>quote_sql()</code> does not provide |
| 857 |
any protection against SQL injection attacks.</p> |
| 858 |
<div class="sourceCode" id="cb46"><pre class="sourceCode jinjar sql bg-info"><code class="sourceCode sql"><span id="cb46-1"><a href="#cb46-1" tabindex="-1"></a><span class="kw">WHERE</span> title <span class="op">=</span> {{ quote_sql(title) }} <span class="kw">AND</span> <span class="dt">year</span> <span class="op">=</span> {{ quote_sql(godzilla.Born) }}</span></code></pre></div> |
| 859 |
<div class="sourceCode" id="cb47"><pre class="sourceCode sql bg-success"><code class="sourceCode sql"><span id="cb47-1"><a href="#cb47-1" tabindex="-1"></a><span class="kw">WHERE</span> title <span class="op">=</span> <span class="st">'My Webpage'</span> <span class="kw">AND</span> <span class="dt">year</span> <span class="op">=</span> <span class="dv">1952</span></span></code></pre></div> |
| 860 |
<p>When passed an array, <code>quote_sql()</code> will quote each |
| 861 |
element and return a comma-separated list. This is particularly helpful |
| 862 |
when using the SQL <code>IN</code> operator.</p> |
| 863 |
<div class="sourceCode" id="cb48"><pre class="sourceCode jinjar sql bg-info"><code class="sourceCode sql"><span id="cb48-1"><a href="#cb48-1" tabindex="-1"></a><span class="kw">WHERE</span> <span class="fu">user</span> <span class="kw">IN</span> ({{ quote_sql(users) }})</span></code></pre></div> |
| 864 |
<div class="sourceCode" id="cb49"><pre class="sourceCode sql bg-success"><code class="sourceCode sql"><span id="cb49-1"><a href="#cb49-1" tabindex="-1"></a><span class="kw">WHERE</span> <span class="fu">user</span> <span class="kw">IN</span> (<span class="st">'User A'</span>, <span class="st">'User B'</span>, <span class="st">'User C'</span>)</span></code></pre></div> |
| 865 |
</div> |
| 866 |
<div id="numeric-data" class="section level3"> |
| 867 |
<h3>Numeric Data</h3> |
| 868 |
<p>You can check if an integer is even or odd, or divisible by some |
| 869 |
other integer. This could be used to make alternating row colors.</p> |
| 870 |
<pre class="jinjar bg-info"><code>{{ even(42) }} |
| 871 |
{{ odd(42) }} |
| 872 |
{{ divisibleBy(42, 7) }}</code></pre> |
| 873 |
<pre class="bg-success"><code>true |
| 874 |
false |
| 875 |
true</code></pre> |
| 876 |
<p>You can round floating point numbers to a specific precision:</p> |
| 877 |
<pre class="jinjar bg-info"><code>{{ round(3.1415, 0) }} |
| 878 |
{{ round(3.1415, 3) }}</code></pre> |
| 879 |
<pre class="bg-success"><code>3 |
| 880 |
3.142</code></pre> |
| 881 |
</div> |
| 882 |
<div id="string-data" class="section level3"> |
| 883 |
<h3>String Data</h3> |
| 884 |
<p>Translate a string to lower case or upper case:</p> |
| 885 |
<pre class="jinjar bg-info"><code>{{ lower("Hello") }} |
| 886 |
{{ upper("Hello") }}</code></pre> |
| 887 |
<pre class="bg-success"><code>hello |
| 888 |
HELLO</code></pre> |
| 889 |
<p>Escape special characters for use in HTML content (see <a href="#html-escaping">HTML Escaping</a>):</p> |
| 890 |
<div class="sourceCode" id="cb56"><pre class="sourceCode jinjar html bg-info"><code class="sourceCode html"><span id="cb56-1"><a href="#cb56-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">input</span><span class="ot"> type</span><span class="op">=</span><span class="st">"text"</span><span class="ot"> value</span><span class="op">=</span><span class="st">"{{ escape_html(name) }}"</span><span class="dt">></span></span></code></pre></div> |
| 891 |
<div class="sourceCode" id="cb57"><pre class="sourceCode html bg-success"><code class="sourceCode html"><span id="cb57-1"><a href="#cb57-1" tabindex="-1"></a><span class="dt"><</span><span class="kw">input</span><span class="ot"> type</span><span class="op">=</span><span class="st">"text"</span><span class="ot"> value</span><span class="op">=</span><span class="st">"Dwayne </span><span class="dv">&quot;</span><span class="st">The Rock</span><span class="dv">&quot;</span><span class="st"> Johnson"</span><span class="dt">></span></span></code></pre></div> |
| 892 |
<p>Quote string data for use as string literals in a SQL query (see <a href="#sql-quoting">SQL Quoting</a>):</p> |
| 893 |
<div class="sourceCode" id="cb58"><pre class="sourceCode jinjar sql bg-info"><code class="sourceCode sql"><span id="cb58-1"><a href="#cb58-1" tabindex="-1"></a><span class="kw">WHERE</span> <span class="fu">user</span> <span class="kw">IN</span> ({{ quote_sql(users) }})</span></code></pre></div> |
| 894 |
<div class="sourceCode" id="cb59"><pre class="sourceCode sql bg-success"><code class="sourceCode sql"><span id="cb59-1"><a href="#cb59-1" tabindex="-1"></a><span class="kw">WHERE</span> <span class="fu">user</span> <span class="kw">IN</span> (<span class="st">'User A'</span>, <span class="st">'User B'</span>, <span class="st">'User C'</span>)</span></code></pre></div> |
| 895 |
</div> |
| 896 |
<div id="json-lists" class="section level3"> |
| 897 |
<h3>JSON Lists</h3> |
| 898 |
<p>Get the number of list elements:</p> |
| 899 |
<pre class="jinjar bg-info"><code>length(): {{ length([3,1,2]) }}</code></pre> |
| 900 |
<pre class="bg-success"><code>length(): 3</code></pre> |
| 901 |
<p>Get the first or last elements:</p> |
| 902 |
<pre class="jinjar bg-info"><code>first(): {{ first([3,1,2]) }} |
| 903 |
last(): {{ last([3,1,2]) }}</code></pre> |
| 904 |
<pre class="bg-success"><code>first(): 3 |
| 905 |
last(): 2</code></pre> |
| 906 |
<p>Get the minimum or maximum elements:</p> |
| 907 |
<pre class="jinjar bg-info"><code>min(): {{ min([3,1,2]) }} |
| 908 |
max(): {{ max([3,1,2]) }}</code></pre> |
| 909 |
<pre class="bg-success"><code>min(): 1 |
| 910 |
max(): 3</code></pre> |
| 911 |
<p>Sort the list into ascending order:</p> |
| 912 |
<pre class="jinjar bg-info"><code>sort(): {{ sort([3,1,2]) }}</code></pre> |
| 913 |
<pre class="bg-success"><code>sort(): [1,2,3]</code></pre> |
| 914 |
<p>Join a list with a separator:</p> |
| 915 |
<pre class="jinjar bg-info"><code>{{ join([1,2,3], " + ") }} |
| 916 |
{{ join(users, ", ") }}</code></pre> |
| 917 |
<pre class="bg-success"><code>1 + 2 + 3 |
| 918 |
User A, User B, User C</code></pre> |
| 919 |
<p>Generate a list as a range of integers:</p> |
| 920 |
<pre class="jinjar bg-info"><code>{% for i in range(4) %}{{ loop.index1 }}{% endfor %}</code></pre> |
| 921 |
<pre class="bg-success"><code>1234</code></pre> |
| 922 |
<p>Access elements using a dynamic index with <code>at()</code>. Note |
| 923 |
that the index is <strong>zero-based</strong>.</p> |
| 924 |
<pre class="jinjar bg-info"><code>{% set x = [1,2,3] -%} |
| 925 |
{% set i = 2 -%} |
| 926 |
{{ x.2 }} |
| 927 |
{{ at(x, i) }}</code></pre> |
| 928 |
<pre class="bg-success"><code>3 |
| 929 |
3</code></pre> |
| 930 |
</div> |
| 931 |
<div id="json-objects" class="section level3"> |
| 932 |
<h3>JSON Objects</h3> |
| 933 |
<p>Access values using a dynamic key with <code>at()</code>:</p> |
| 934 |
<pre class="jinjar bg-info"><code>{% set x = {"a": 1, "b": 2} -%} |
| 935 |
{% set key = "b" -%} |
| 936 |
{{ x.b }} |
| 937 |
{{ at(x, key) }}</code></pre> |
| 938 |
<pre class="bg-success"><code>2 |
| 939 |
2</code></pre> |
| 940 |
</div> |
| 941 |
</div> |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
<script> |
| 950 |
(function () { |
| 951 |
var script = document.createElement("script"); |
| 952 |
script.type = "text/javascript"; |
| 953 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 954 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 955 |
})(); |
| 956 |
</script> |
| 957 |
|
| 958 |
</body> |
| 959 |
</html> |