Display functions

Display functions turn a value into text for the page. They are plain calls, with the value first:

  • { currency(album.price) } prints $28.00.
  • { pluralize(albums.length, 'album') } prints the count with the word: 1 album, 3 albums.
  • { date(album.released, 'long') } prints April 12, 2019; 'short' and 'medium' are the other presets.
  • { truncate(text, 60) } cuts text to 60 characters, ending in ….
  • { raw(album.notes) } renders HTML instead of escaping it, through a sanitizer that drops scripts, styles and event attributes.

Calls nest, so { truncate(strip_html(album.notes), 60) } drops the tags first, then shortens. There are more: number_with_delimiter, percentage, capitalize, time, timeago and others.

Your own functions work the same way. Register them under formatters in app.js and call them by name in any template. Keep them pure: same input, same output.

formatters: {
  duration: (seconds) => `${Math.round(seconds / 60)} min`,
},

Your turn

  1. Shop.pzl: show each card's price with currency(album.price) in a <span class="price"> above the button.
  2. Playlist.pzl: the count says "1 albums". Use pluralize(albums.length, 'album'). Then replace the artist under each title with the notes, tags removed and cut to 60 characters.
  3. Album.pzl: print the release date with date(album.released, 'long'), and render the notes with raw(). They show their <em> tags as text until you do.
  4. app.js: register duration, then use it in Album.pzl to print the album's length after the track count: { pluralize(tracks.length, 'track') } · { duration(totalSeconds) }. data() already adds up totalSeconds.

Chapters