You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/dynamic-metadata.qmd
+88Lines changed: 88 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -94,6 +94,94 @@ Generated at: {{< meta generated_at >}}
94
94
This content would be hidden when debug mode is enabled.
95
95
:::
96
96
97
+
## Advanced Use Case: Conditional Content Based on parameters
98
+
99
+
Another powerful use case is making Quarto parameters available as metadata for conditional content. This allows you to control document behavior through parameters while leveraging Quarto's conditional content features.
100
+
101
+
Here's an example that demonstrates creating different versions of a sales report based on parameters:
102
+
103
+
````markdown
104
+
---
105
+
title: "Sales Report"
106
+
format: html
107
+
params:
108
+
region: "North America"
109
+
show_confidential: false
110
+
quarter: "Q1"
111
+
---
112
+
113
+
```{{r}}
114
+
#| echo: false
115
+
#| output: asis
116
+
quarto::write_yaml_metadata_block(
117
+
params = params
118
+
)
119
+
```
120
+
121
+
# {{< meta params.quarter >}} Sales Report - {{< meta params.region >}}
1.**Parameterized reporting**: Generate different document versions based on input parameters
159
+
2.**Conditional content**: Show or hide sections dynamically based on computed values
160
+
3.**Document customization**: Tailor content and presentation for different contexts or audiences
161
+
4.**Workflow automation**: Control document behavior programmatically through parameter passing
162
+
163
+
You can render different versions by passing parameters:
164
+
165
+
```r
166
+
# Internal report with confidential data
167
+
quarto::quarto_render("sales-report.qmd",
168
+
execute_params=list(
169
+
region="North America",
170
+
show_confidential=TRUE,
171
+
quarter="Q2"
172
+
))
173
+
174
+
# Public report without confidential data
175
+
quarto::quarto_render("sales-report.qmd",
176
+
execute_params=list(
177
+
region="Europe",
178
+
show_confidential=FALSE,
179
+
quarter="Q2"
180
+
))
181
+
```
182
+
183
+
The key insight is that `write_yaml_metadata_block(params = params)` makes all your document parameters available as metadata. The boolean ones can then be used with Quarto's `when-meta` and `unless-meta` conditional attributes for dynamic content control.
184
+
97
185
## Advanced Use Case: Email Variant Testing
98
186
99
187
One powerful application of dynamic metadata is variant emails using Quarto's email format. This example shows how to randomly select an email variant and conditionally display different content based on that selection:
0 commit comments