Skip to content

Provide a helper function to create metadata yaml block in document body #137

Open
@cderv

Description

@cderv

Related to #160

cc @kmasiello

Example

---
title: "Some title"
format: html
---

```{r}
admin <- TRUE
```


```{r}
#| output: asis
cat(
  "---",
  paste0("admin: ", ifelse(admin, "true", "false")),
  "---",
  sep = "\n"
)
```

Is this document for admin: {{< meta admin >}}


::: {.content-visible when-meta="admin"}
If so, then this should be visible as it is only `when-meta="admin"`
:::

::: {.content-hidden when-meta="admin"}
Hum... It seems you are not admin..
:::

This is quite long to write, so it could use a helper

Using inline R code

---
title: "Some title"
format: html
---

```{r}
admin <- TRUE
```


```{r}
write_meta <- function(meta) {
  handlers <- list(logical = function(x) {
        value <- ifelse(x, "true", "false")
        structure(value, class = "verbatim")
    })
  res <- yaml::as.yaml(meta, handlers = handlers)
  knitr::asis_output(paste0("---\n", res, "---\n"))
}
```


`r write_meta(list(admin = TRUE))`


Is this document for admin: {{< meta admin >}}


::: {.content-visible when-meta="admin"}
If so, then this should be visible as it is only `when-meta="admin"`
:::

::: {.content-hidden when-meta="admin"}
Hum... It seems you are not admin..
:::

Or using a cell with option

---
title: "Some title"
format: html
keep-md: true
---

```{r}
admin <- TRUE
```


```{r}
write_meta <- function(meta) {
  handlers <- list(logical = function(x) {
        value <- ifelse(x, "true", "false")
        structure(value, class = "verbatim")
    })
  res <- yaml::as.yaml(meta, handlers = handlers)
  knitr::asis_output(paste0("---\n", res, "---\n"))
}
```

```{r}
#| echo: false
#| output: asis
write_meta(list(admin = TRUE))
```

Is this document for admin: {{< meta admin >}}


::: {.content-visible when-meta="admin"}
If so, then this should be visible as it is only `when-meta="admin"`
:::

::: {.content-hidden when-meta="admin"}
Hum... It seems you are not admin..
:::

Food for thoughts...

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions