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

jsstencil

v1.0.5

Published

JsStencil - ExpressJS Javascript Template for Dynamic Webpage generation

Downloads

7

Readme

JsStencil

ExpressJS Javascript Template for Dynamic Webpage generation

Installation

$ npm install jsstencil

Features

  • Control flow with <% %>.
  • Html output with <%= %> or echo.
  • Html-safe output with <%= htmlentities(...) %>.
  • Complete access to request (req) and response (res) objects.
  • Supports require to include NodeJS modules or nested templates.
  • Supports user-defined functions and values.

Examples

...
<% if (user) { %>
  <span><%= user.name %></span>
<% } %>
...

A more complex sample that uses express-session:

<!DOCTYPE html>
<html>
<head>
	<% require('./header.jss'); %>
	<title>Sample</title>
</head>
<body>
<%
var fs = require('fs');
var fileName = fs.statSync('/tmp/sample-file.txt')
try {
	stat = fs.statSync(fileName);
}
catch (e) {
	stat = undefined;
}
%>
	<div>
		<%
		if (!req.session.value)
			req.session.value = 1;
		else
			req.session.value++;
		%>
		<p>Session value: <% echo(req.session.value.toString()); %></p>
		<%
		if (stat && stat.isFile()) {
		%>
		<p>sample-file.txt does exist!!</p>
		<%
		}
		%>
	</div>
</body>
</html>
<%
done();
%>

IMPORTANT: Because the templates are parsed as javascript files, ensure your code is safe to avoid introducing security issues.

Usage

var jstRenderer = require('jsstencil').createRenderer(options);
//...
app.get('*', function(req, res, next) {
		jstRenderer(req, res, next); 
	},
	//renderer will pass the request to 'next' if not a .jss file
	function(req, res, next) {
		//...
	}
);

Although jstRenderer value can be used as a template view engine, its functionallity will be limited.

The special function done()

All scripts (.jss) must call done(); or done(err); when they finish to execute the code.

Given the asynchronous nature of NodeJS, part of the user script may need to wait an asyncronous response, for e.g.: from a database, to render some data so the script must advise the engine when it finishes to do its tasks.

The done function tells JsStencil that rendering is complete so it can continue executing.

If your code has no asynchronous tasks, simply add the call to done(); at the end. Else call it when your asynchronous callback is called.

If you specify the optional error paremeter, a http status 500 Internal Server Error will be sent to the browser including the details of the error.

Options

  • root (string) Project root for includes with an absolute path (/index.jss).

  • showErrorDetails (boolean) Send details with the 500 Internal Server Error status on JS errors.

  • fileExt (string) File extension to use (default: .jss)

  • userData (object) A set of values and/or functions to pass to the template. Object keys will appear as global objects in the template.

    For example, if you use:

    userData: {
        mysql: require('mysql'),
        intValue: 5
        sum: function (a, b) {
            return a + b;
        }
    }

    Then you can do something like this:

    <%
    var a = sum(5, intValue);
    %>

Require/includes

When you use require using a relative path and the file extension matches options.fileExt (which defaults to .jss), require will act as an include command and the content of the file will be inserted at the current script position.

Otherwise require will have its default NodeJS behavior.

Limitations

  • Currently, there is no caching scheme.
  • Although JsStencil can be used as a view engine, it is not fully tested and have limitations, like req and res to be unavailable.
  • No client-side support.

License

Licensed under the MIT License (https://opensource.org/licenses/MIT)


JsStencil - Javascript Template for Dynamic Webpage generation Copyright (C) 2017 mxmauro [at] mauroleggieri [dot] com