quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.6k stars 294 forks source link

Filtering items of listings based on meta data #2833

Closed dragonstyle closed 1 year ago

dragonstyle commented 1 year ago

Discussed in https://github.com/quarto-dev/quarto-cli/discussions/2828

Originally posted by **mllg** October 13, 2022 I have a folder `publications` which contains a `qmd` for multiple publications. Each `qmd` file comes with meta data about the publication in the front matter. I can now easily list all publications, e.g. with ```yaml listing: contents: - "publications/*.qmd" ``` I now want to provide a different view on this listing, i.e. restrict the listing to include only publications of a specific author. I can do this with a custom listing and an ejs template which filters on pattern `"NAME"`: ```ejs
    <% for (const item of items) { %> <% if(item.author.match("NAME") ){ %>
  • > <%= item.title %>.
  • <% } %> <% } %>
``` I'm wondering if there is a way to 1. retrieve the `"NAME"` pattern from the meta data of the page which is contains the listing, or 2. a different way to pass down variables to ejs templates?
dragonstyle commented 1 year ago

This is now supported via template-params, which can be passed to a listing like so:

listing:
  contents: posts
  template: my-custom-template.ejs
  template-params:
    author: "NAME"

and referenced like:

<ul class="pub-list list">
  <% for (const item of items) { %>
    <% if(!templateParams.name || item.author.match(templateParams.name) ){ %>
      <li <%= metadataAttrs(item) %>>
        <span class="listing-title"><%= item.title %>.</span>
      </li>
    <% } %>
  <% } %>
</ul>