Skip to content

FR: conditional content based on metadata #5707

@aronatkins

Description

@aronatkins

Bug description

Using Quarto 1.3.340 on macOS.

Given a website with the following layout:

_quarto.yml
alpha.qmd
beta.qmd
index.qmd
include/
    _common.qmd
    _default_details.qmd
    _other_details.qmd

The quarto.yml:

project:
  type: website

website:
  navbar:
    left:
      - text: "Home"
        file: index.qmd
      - text: "alpha"
        file: alpha.qmd
      - text: "beta"
        file: beta.qmd

The contents of index.qmd are not interesting.

Here is alpha.qmd

---
title: alpha
---

Alpha is first. Consider nothing else.

```{r, include = FALSE}
PROFILE <- "default"
```

```{r, child = "include/_common.qmd"}
```

There is nothing else to say about alpha.

Here is beta.qmd

---
title: beta
---

Beta is a hammer bot.

```{r, include = FALSE}
PROFILE <- "beta"
```

```{r, child = "include/_common.qmd"}
```

The common stuff is out of the way. Give thanks to beta.

The include/_common.qmd needs to use two conditional knitr chunks:

This is the common include file. We are the `r#PROFILE` profile.

-   Be prepared. Some very interesting text is about to appear.

```{r, child=if(PROFILE=="default") "include/_default_details.qmd"}
```

```{r, child=if(PROFILE!="default") "include/_other_details.qmd"}
```

After we gave you all of that, is there anything more?

The two nested include files are fairly simple.

The include/_default_details.qmd:

-   The details you get by default are amazing.

The include/_other_details.qmd:

-   When you are not the default, you can often feel left behind.

This is quite a lot of complexity, especially when include/_common.qmd contains many conditional blocks.

It would be good to avoid the need for R and knitr to accomplish:

  1. Per-document variables that can be used by included files for text replacement with variable short-codes.
  2. Per-document variables that can be used by included files to make decisions about what text to include in the render.

This example was the result of porting an include file from mkdocs that looked like:

This is the common include file. We are the {{ PROFILE }} profile.

-   Be prepared. Some very interesting text is about to appear.

{% if PROFILE == "default" %}
-   The details you get by default are amazing.
{% else %}
-   When you are not the default, you can often feel left behind.
{% endif %}

After we gave you all of that, is there anything more?

Steps to reproduce

No response

Expected behavior

No response

Actual behavior

No response

Your environment

No response

Quarto check output

No response

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions