| 1 |
--- |
| 2 |
title: "Auxiliary Templates" |
| 3 |
output: rmarkdown::html_vignette |
| 4 |
vignette: > |
| 5 |
%\VignetteIndexEntry{Auxiliary Templates} |
| 6 |
%\VignetteEngine{knitr::rmarkdown} |
| 7 |
%\VignetteEncoding{UTF-8} |
| 8 |
--- |
| 9 |
|
| 10 |
```{r, include=FALSE} |
| 11 |
knitr::opts_chunk$set( |
| 12 |
collapse = TRUE, |
| 13 |
comment = "#>" |
| 14 |
) |
| 15 |
``` |
| 16 |
|
| 17 |
```{r setup, include=FALSE} |
| 18 |
library(jinjar) |
| 19 |
``` |
| 20 |
|
| 21 |
As the complexity of your templating project grows, it can be helpful to refactor common pieces into auxiliary templates so they can be reused. |
| 22 |
The [`{% include %}`](#include) and [`{% extends %}`](#extends) tags support two very different approaches to this, described below. |
| 23 |
|
| 24 |
|
| 25 |
## Loading Auxiliary Templates |
| 26 |
|
| 27 |
When your main template makes reference to auxiliary templates, you'll need to specify how the templating engine can find these auxiliary templates. |
| 28 |
This is achieved using a template loader (see `help("loader")`). |
| 29 |
|
| 30 |
There are different types of loader, but `path_loader()` is the most commonly used. |
| 31 |
This allows you to specify the directory where auxiliary templates are stored in files. |
| 32 |
|
| 33 |
Imagine you have a main template that uses nested template inheritance (`content` inherits from `blog_post.html`, which in turns inherits from `base.html`). |
| 34 |
You might store these two auxiliary templates in a templates directory: |
| 35 |
|
| 36 |
```shell |
| 37 |
/path/to/templates/ |
| 38 |
|-- base.html |
| 39 |
|-- blog_post.html |
| 40 |
``` |
| 41 |
|
| 42 |
When rendering the main template, you create the loader object as part of the engine configuration: |
| 43 |
|
| 44 |
```{r, eval=FALSE} |
| 45 |
config <- jinjar_config(loader = path_loader("path", "to", "templates")) |
| 46 |
output <- render(content, !!!data, .config = config) |
| 47 |
``` |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
## Template Inclusion {#include} |
| 52 |
|
| 53 |
The `include` tag can be used to include an auxiliary template and return the rendered contents of that file into the main document. |
| 54 |
Included templates have access to the same variables as the main template. |
| 55 |
|
| 56 |
By default, an error is raised if the included template cannot be found. |
| 57 |
You can ignore these errors by setting the `ignore_missing_files` argument in `jinjar_config()`. |
| 58 |
|
| 59 |
As an example, we create an auxiliary file `header.html` with contents: |
| 60 |
|
| 61 |
```{cat, engine.opts=list(file="header.html", lang="html")} |
| 62 |
<!DOCTYPE html> |
| 63 |
<html lang="en"> |
| 64 |
<head> |
| 65 |
<title>My webpage</title> |
| 66 |
</head> |
| 67 |
``` |
| 68 |
|
| 69 |
and an auxiliary file `footer.html` with contents: |
| 70 |
|
| 71 |
```{cat, engine.opts=list(file="footer.html", lang="html")} |
| 72 |
</html> |
| 73 |
``` |
| 74 |
|
| 75 |
And then the main template is rendered as: |
| 76 |
|
| 77 |
```{jinjar, engine.opts=list(lang="html", config=jinjar_config(getwd()))} |
| 78 |
{% include "header.html" %} |
| 79 |
<body> |
| 80 |
Body |
| 81 |
</body> |
| 82 |
{% include "footer.html" %} |
| 83 |
``` |
| 84 |
|
| 85 |
```{r clean_include, include=FALSE} |
| 86 |
unlink(c("header.html", "footer.html")) |
| 87 |
``` |
| 88 |
|
| 89 |
|
| 90 |
## Template Inheritance {#extends} |
| 91 |
|
| 92 |
Template inheritance allows you to build a base "skeleton" template that contains all the common elements of your document and defines _blocks_ that child templates can override. |
| 93 |
This is a very powerful technique. |
| 94 |
|
| 95 |
As an example, consider the following base template stored in `base.html`: |
| 96 |
|
| 97 |
```{cat, engine.opts=list(file="base.html", lang="html")} |
| 98 |
<!DOCTYPE html> |
| 99 |
<html> |
| 100 |
<head> |
| 101 |
{% block head -%} |
| 102 |
<link rel="stylesheet" href="style.css" /> |
| 103 |
<title>{% block title %}{% endblock %} - My Webpage</title> |
| 104 |
{% endblock %} |
| 105 |
</head> |
| 106 |
<body> |
| 107 |
<div id="content">{% block content %}{% endblock %}</div> |
| 108 |
</body> |
| 109 |
</html> |
| 110 |
``` |
| 111 |
|
| 112 |
This base template declares three `{% block %}` tags that child templates can fill in: `head`, `title` and `content`. |
| 113 |
Note that the base template itself defines some content for the `head` block -- we'll show how a child template can use this below. |
| 114 |
|
| 115 |
A child template uses the `{% extends %}` tag to declare which parent template it builds upon. |
| 116 |
This should be the first tag in the child template, so the templating engine knows it must locate the parent template when rendering. |
| 117 |
|
| 118 |
Building upon the base template example above, a child template might look like this: |
| 119 |
|
| 120 |
```{jinjar, engine.opts=list(lang="html", config=jinjar_config(getwd()))} |
| 121 |
{% extends "base.html" %} |
| 122 |
{% block title %}Index{% endblock %} |
| 123 |
{% block head %} |
| 124 |
{{ super() }} |
| 125 |
<style type="text/css"> |
| 126 |
.important { color: #336699; } |
| 127 |
</style> |
| 128 |
{% endblock %} |
| 129 |
{% block content %} |
| 130 |
<h1>Index</h1> |
| 131 |
<p class="important"> |
| 132 |
Welcome to my blog! |
| 133 |
</p> |
| 134 |
{% endblock %} |
| 135 |
``` |
| 136 |
|
| 137 |
This child template defines the three blocks declared by the parent template: `head`, `title` and `content`. |
| 138 |
In the case of the `head` block, it uses `{{ super() }}` to render the contents of the `head` block defined by the parent template. |
| 139 |
If we were using nested `extends` tags, we could pass an argument to skip levels in the inheritance tree (e.g. `{{ super(2) }}`). |
| 140 |
|
| 141 |
```{r clean_extends, include=FALSE} |
| 142 |
unlink(c("base.html")) |
| 143 |
``` |