diff --git a/docs/websites/website-listings-custom.qmd b/docs/websites/website-listings-custom.qmd index 47667ee805..ae3306b86a 100644 --- a/docs/websites/website-listings-custom.qmd +++ b/docs/websites/website-listings-custom.qmd @@ -4,62 +4,80 @@ title: Custom Listings ## Overview -In addition to the 3 built in types of listings, you can also build a completely custom display of the items. This custom display can generate any HTML and can optionally still take advantage of the sorting, filtering, and pagination provided by listings. +In addition to the three built-in types of listings, you can also build a completely custom display of the items. This custom display can generate any HTML and can optionally still take advantage of the sorting, filtering, and pagination provided by listings. ## Listing Templates -To build a custom listing display, you create an [EJS template](https://ejs.co) that will be used to generate the HTML for a set of items that are passed to the template. EJS templates allow you to generate HTML using plain javascript, making it easy to loop through items and output their values in your custom HTML. +To use a custom template, add the `template` option to your listing configuration: - -To use a custom template, pass it in the `template` option for a listing: - -``` yaml -listing: - template: gallery.ejs -``` - -When a listing with a custom template is rendered, the listing contents will be read and processed into a set of items that are passed to the template for rendering. For example, in this case, all the documents in the posts directory will be read into items and passed to the `gallery.ejs` template. - -``` yaml +```{.yaml filename="index.qmd"} +--- listing: contents: posts - template: gallery.ejs + template: gallery.ejs.md +--- ``` -A simple template for outputing a list of documents might look like: +Quarto passes the listing contents to the template as an array called `items`. +Each item is populated with the fields described in [Listing Fields](website-listings.qmd#listing-fields), plus any custom metadata from the document. + +The template uses [EJS](https://ejs.co) to combine HTML and JavaScript (inside `<% %>` tags) to process `items`. +For example, this template constructs a list of links by looping through `items` and accessing the `path` and `title` fields: -``` html +````{.html filename="gallery.ejs.md"} +```{=html} ``` +```` -which produces simple HTML output like: +The result is a simple bulleted list of links to the documents in the listing: -![](images/listing-custom-output.png){.border} +![](images/listing-custom-output.png){.border fig-alt="A custom listing output showing a list of document titles as links."} -When rendered, the above template will receive an array of listing items called `items`. When the contents of a listing are loaded from a list of documents, each of those items will be populated with the fields described in [Listing Fields](website-listings.qmd#listing-fields). In addition, any other fields included in a documents metadata will be passed as a property of the item, making it possible to use custom metadata in your documents and the listing display. -::: {.callout-note} +Quarto processes EJS templates as Markdown, so we recommend using the `.ejs.md` extension for your template files. Since most templates output HTML, wrap your HTML in a raw block (`` ```{=html} ``) to avoid performance issues and escaping problems. -Note that Quarto uses `lodash` to render the EJS templates. The `lodash` uses different syntax for HTML escaping text in templates. +If you want a field like `title` to support Markdown formatting (*e.g.*, `My **bold** title`), exit the raw HTML block to output the Markdown content, then re-enter it: +````{.html filename="gallery.ejs.md"} +```{=html} + +``` ```` + +::: {.callout-note} +## HTML Escaping + +Use `<%- value %>` to escape HTML characters in a value, or `<%= value %>` to output without escaping: + +````ejs HTML escaped value: <%- value %> HTML unescaped value: <%= value %> ```` +This syntax comes from `lodash`, which Quarto uses to render EJS templates. ::: ## Metadata Listings The `contents` option for a listing most commonly contains a list of paths or globs, but it can also contain metadata. When contents are metadata, the metadata will be read into items and passed to the template. For example: -``` yaml +```{.yaml filename="index.qmd"} +--- listing: - template: custom.ejs + template: custom.ejs.md contents: - name: First Item href: https://www.quarto.org @@ -67,11 +85,12 @@ listing: - name: Second Item href: https://www.rstudio.org custom-field: A second custom value +--- ``` could be rendered using: -```` html +````{.markdown filename="custom.ejs.md"} ```{=html}