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

pup-tent

v0.9.10

Published

File caching and template rendering femto-framework using mustache and some assumptions.

Downloads

39

Readme

pup-tent

File caching and template rendering femto-framework using mustache and some assumptions.

On GitHub

NPM

API docs

pup-tent is maintained by Berkeley BOP & kltm

Overview

Pup Tent is a femto-framework for template (Mustache) and static content delivery for Node.js and RingoJS (beta) web apps.

The idea is to quickly turn a pile of related JS, template, CSS, and static files into a whole coherent enough to deliver with a proper web app/routing framework, such as express.

There are two main aspects to Pup Tent. The first is searching for and synchronously caching static content on the filesystem, (including caching templates for later use), and then producing those when given the filename key (all filenames must be unique in Pup Tent). The second is aiding in using a couple of common template patterns using Mustache, examples below.

Usage

The Basics (kicking the tires, lightly)

There are other examples in the tests/ directory, but a full example (without using the cache) might look like this.

Lets say you have the following files:

  • static/frame.tmpl
  • static/content.tmpl
  • static/bar.css
  • static/foo.js
  • static/App.js

The file content.tmpl looks like:

{{ content }}

And the file frame.tmpl looks like:

<html>
  <head>
    <title>{{title}}</title>
    {{#pup_tent_css_libraries}}
    <link rel="stylesheet" type="text/css" href="{{{.}}}">
    {{/pup_tent_css_libraries}}
    {{#pup_tent_js_variables}}
    <script type="text/javascript">var {{name}} = {{{value}}};</script>
    {{/pup_tent_js_variables}}
    {{#pup_tent_js_libraries}}
    <script type="text/javascript" src="{{{.}}}"></script>
    {{/pup_tent_js_libraries}}
  </head>
  <body>
  {{ &pup_tent_content }}
  </body>
</html>

The easiest way to deploy my App.js using this template and file structure could be something like:

var pup_tent = require('pup-tent')(['static']);

Set the common variables:

pup_tent.set_common('js_vars', {'name': 'foo', 'value': 'bar'});
pup_tent.set_common('js_libs', 'foo.js');
pup_tent.set_common('css_libs', 'bar.css');

Set the variables for just this page:

var targs = {content: 'bar', title: 'foo', 'pup_tent_js_libraries': ['App.js']};
var output = pup_tent.render('content.tmpl', targs, 'frame.tmpl');

This would give output like:

<html>
  <head>
    <title>foo</title>
    <link rel="stylesheet" type="text/css" href="bar.css">
    <script type="text/javascript">var foo = "bar";</script>
    <script type="text/javascript" src="foo.js"></script>
    <script type="text/javascript" src="App.js"></script>
  </head>
  <body>
  bar
  </body>
</html>

You might notice that some of the files are not correctly "linking" if you view your page in a web browser. On to the next section!

More Advanced (test drive)

So far, we've just used it for some simple templating, but what if we want to use the integrated caching mechanism and use it in conjunction with something like express?

A more full-bodied example, using express as an example, might be:

var us = require('underscore');
var express = require('express');
var fs = require('fs');
var mustache = require('mustache')
var pup_tent = require('pup-tent')

var app = express();

//////
/// FOLLOW THE EXAMPLE ABOVE FOR REDACTED STUFF HERE ///
//////

// Pretty much the same as before.
app.get('/', function(req, res){
    var targs = {
	'title': 'Testing'
    };
    var output = pup_tent.render('content.tmpl', targs, 'frame.tmpl');
    res.send(output);
});

// Cached static routes, using Pup Tent to easily deliver static docs.
var js_re = /\.js$/;
var css_re = /\.css$/;
var html_re = /\.html$/;
// Routes for all static cache items at top-level.
us.each(pup_tent.cached_list('flat'), function(thing){
    var ctype = null;
    if( js_re.test(thing) ){
        ctype = 'text/javascript';
    }else if( css_re.test(thing) ){
        ctype = 'text/css';
    }else if( html_re.test(thing) ){
        ctype = 'text/html';
    }
    
    // This will skip cached templates.
    if( ctype !== null ){
        app.get('/' + thing, function(req, res) {
            res.setHeader('Content-Type', ctype);
            res.send(pup_tent.get(thing) );
        });
    }
});

var server = app.listen(3333, function() {
    console.log('Starting at http://localhost:' +
		server.address().port);
});

Since always caching can be a bit annoying when developing JavaScript/CSS heavy websites, there is also a use_cache_p() function that can be used to toggle whether Pup Tent returns to the filesystem every time or uses the internal cache. For more information about everything, see the API docs (linked at the end).

Special variables

The special stack variables are:

  • css_libs: will map to pup_tent_css_libraries
  • js_vars: will map to pup_tent_js_variables
  • js_libs: will map to pup_tent_js_libraries

The special template variables are:

  • pup_tent_css_libraries: list of CSS files to use
  • pup_tent_js_libraries: list of JS files to use
  • pup_tent_js_variables: list of name/value objects to convert to vaiables
  • pup_tent_content: meant for use in base_tmpl_name to embed one template in another

Tests

Node.js

Given the right environment, the tests can be easily run from the
command line using the gulpfile.js.
gulp test

RingoJS

The RingoJS tests are not integrated with the Makefile (yet), but can be run from the command line interface like:

ringo -m ./lib -m ./node_modules/underscore/ -m node_modules/mustache tests/full-tmpl.js.tests