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

view

v1.1.1

Published

A View engine

Downloads

4,307

Readme

view

A View-Presenter pattern for JavaScript.

Installation

npm install view

What is this project?

The view module provides a simple View-Presenter pattern for creating views using a template and a single transformational JavaScript method.

The template is the "View", the single JavaScript method is the "Presenter".

This simple pattern allows for a very clean View which can easily be extended with any advanced features you might want.

It ships with templating support for HTML or Markdown, but can easily be extended with the templating engine of your choice.

The project is well tested with easy to read test cases. It's been in development for several years and is used in many production applications such as hook.io

Features

Hierarchy Views mapped directly to the file-system

The structure of the folder which contains the view maps directly to the constructed View object.

For example:

/view
  index.html
  /about
   index.html
   team.html
  contact.html

Will map to:

// renders View-Presenter
myView.index.present(opts, cb); 
myView.about.index.present(opts, cb);
myView.about.team.present(opts, cb);
myView.contact.present(opts, cb);

// also can introspect templates as strings
myView.index.template;
myView.about.index.template;
myView.about.team.template;

Isomorphic $ based Presenter Pattern

A Presenter is a single optional JavaScript method which can be associated with a View template.

Presenters are paired with a View by existing in the same directory and sharing the same name as the View template.

When View.present() is called, this Presenter method will execute on the context of the View template.

For example:

/view
  index.html
  index.js
  about/
   index.html
   team.html
   team.js
  contact.html
module['exports'] = function examplePresenter(opts, callback) {
  // this.$ is a jQuery like selector with it's HTML context set to the current `View.template`
  var $ = this.$;

  // you can perform normal jQuery like modifications here
  $('h1').html('Hello');
  
  // once you are done transforming the template, continue with the newly modified html
  // notice this uses a callback? this means you can retrieve data from any remote source!
  // simply require the module you need in this Presenter and call out.

  callback(null, $.html());

}

Our unit tests will give a much better idea on how this works in practice.

Layouts / Nested Layouts

A simple layout pattern is available for Views.

If a View has a layout.html or layout.js file present, that layout file will yield the results of the View-Presenter into the layout.

If no layout is found at the current level of the View-Presenter tree, the View will recurse upwards until a layout or the root is found.

For example:

/view
  index.html
  layout.html
  layout.js
  /about
   index.html
   team.html
   team.js
  contact.html

layout.html

<h1>Hello</h1>
<div class="yield">
</div>
// Will yield the contents of the "index" View inside the ".yield" element
myView.index.present(opts, cb); 

// In the case of a nested view, the layout will still be applied
myView.about.team.present(opts, cb);
myView.about.index.present(opts, cb);
myView.contact.present(opts, cb);

Our unit tests will also give a better idea on how nested layouts will be applied.

Built-in Node.js HTTP middleware

The view module ships with a built-in Node.js middleware. This middleware will map the View to incoming urls of your web application.

For example:

/view
  index.html
  about/
   index.html
   team.html
  contact.html
  hello.markdown
 // for simplicity, assume server is an Express.js application
 server.use(view.middle({view: myView }));

Will now map to the following urls on your server:

http://localhost/index
http://localhost/about
http://localhost/about/team
http://localhost/contact
http://localhost/hello

Tests

tap test/*.*