npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tuft/view-responder

v2.0.1

Published

Template engine support for Tuft.

Downloads

6

Readme

View Responder

CI Coverage Status Known Vulnerabilities npm

View Responder is a first-party extension for Tuft that allows the use of template engines for rendering views. At present, the following template engines are supported:

For detailed information on how Tuft responders work, view the official documentation.

Installation

  $ npm install @tuft/view-responder

Starting from version 2.0.0, the EJS and Pug template engines are listed as peer dependencies and must be manually installed in addition to View Responder.

  $ npm install ejs

OR

  $ npm install pug

Breaking Changes

Prior to version 2.0.0, View Responder exported a single named function called createViewResponder() which was passed the name of the desired view engine to use, and both EJS and Pug were listed as package dependencies.

From version 2.0.0 onwards, separate createEjsResponder() and createPugResponder() functions are exported instead, and the desired view engine must be installed manually. Unlike createViewResponder(), both of these functions are async functions and must be called with the await keyword.

Usage

Import either the named createEjsResponder function or the createPugResponder function, and then invoke it to create a Tuft responder that can be inserted into any Tuft application. The responder will be triggered by any Tuft response object that contains a render property.

For example, to enable the EJS template engine, call the createEjsResponder function.

Note: createEjsResponder() and createPugResponder() are async functions and require the await keyword.

  // index.js

  const { tuft } = require('tuft')
  const { createEjsResponder } = require('@tuft/view-responder')

  const app = tuft({
    responders: [await createEjsResponder()]
  })

Create an EJS template file like the one below.

<!-- views/index.ejs -->

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
  <head>
  <body>
    <p>Welcome to <%= title %></p>
  </body>
</html>

To render this view, provide a Tuft response object with a render property, passing the name of the '*.ejs' file. If the template requires interpolated data, like in this example, make sure you include it via the data property.

  // index.js

  app.set('GET /', () => {
    return {
      render: 'views/index', // File extension is not required
      data: { title: 'Tuft' }
    }
  })

The example above will respond with the following HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>Tuft</title>
  <head>
  <body>
    <p>Welcome to Tuft</p>
  </body>
</html>

API

createEjsResponder([baseDir])

You can pass a base directory for your view files as an optional first argument. For example, if your index view is located at 'views/index.ejs', you can pass 'views' as the second argument. You then only have to refer to the 'index.ejs' file in your Tuft response.

  const app = tuft({
    responders: [await createEjsResponder('views')] // Include base directory 'views'
  })

  app.set('GET /', () => {
    return {
      render: 'index', // Render 'views/index.ejs'
      data: { title: 'Tuft' }
    }
  })

createPugResponder([baseDir])

You can pass a base directory for your view files as an optional first argument. For example, if your index view is located at 'views/index.pug', you can pass 'views' as the second argument. You then only have to refer to the 'index.pug' file in your Tuft response.

  const app = tuft({
    responders: [await createPugResponder('views')] // Include base directory 'views'
  })

  app.set('GET /', () => {
    return {
      render: 'index', // Render 'views/index.pug'
      data: { title: 'Tuft' }
    }
  })

View Responders recognize the following two properties in a response object:

  • render - Path to a template file.
  • data - An object containing the data to be inserted into the rendered HTML.

View Responders are only triggered by the presence of the render property. If absent, control of the response will be handed back to the Tuft application.

The data property is required only if the template requires interpolated data.

People

The creator and maintainer of View Responder is Stuart Kennedy.

License

MIT