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

jstc

v1.1.0

Published

Lightweight, non-sandboxing JSP-ish template compiler

Downloads

1

Readme

node-jstc v1.1.0

Build Status Coverage Status

Lightweight, non-sandboxing JSP-ish template compiler.

This build-time tool was created after having elected not to fork the excellent, but abandoned doT.js because lightness trumps sandboxing in a project where one writes one's own templates. The general concept was inspired by a templating concept from John Resig (creator of jQuery), with added statements and sessions. Unlike doT.js, there is no syntactic sugar for partials, conditionals or looping through arrays.

Templates are compiled into functions which return a string. Since templates are executed in the current environment, they have access to whatever modules you may have already loaded.

Installation

npm install jstc

Usage

Basic:

jstc templates/*.jst >render.js

The resulting CommonJS module exports an object in which the base name of each template exists as a method which takes one optional argument, available from within the template as it just like in doT.js.

For example, if one of the files was called templates/foo.jst then you could display the template with:

var render = require('./render.js');
var htmlSnippet = render.foo();

Global client-side:

If you want to use render directly old-school in the browser:

jstc templates/*.jst >render.js
browserify --standalone render render.js -o render-standalone.js
<script type="text/javascript" src="render-standalone.js"></script>
<script type="text/javascript">
document.write(render.foo());
</script>

With plugin namespace support:

jstc templates/*.jst plugins/*/templates/*.jst

The pattern 'plugins/*/templates/*.jst' is specifically recognized at compile time and creates one property per plugin directory found in the resulting list of files, and creates methods inside each.

For example, if one file was named plugins/foo/templates/bar.jst you could display it with:

var render = require('./render.js');
var htmlSnippet = render.foo.bar();

Current call object

Template functions take an optional object argument which is available within templates as it. This is fundamental to using templates in a meanginful way.

Session library object

Template functions also have a built-in object lib which is hidden inside render (or whatever name you give the resulting object) and thus available throughout the life of the program. You can expose anything you want to make globally available throughout your templates with render.expose():

<!-- This is template: 'foo' -->
<p><%= lib.echo(it.title) %></p>
var render = require('./render.js');
render.expose('echo', function(s) { return s + s; });
render.foo({title: 'Testing'});  // '<p>TestingTesting</p>'

This lib comes with the following built-in:

lib.tohtml(string)

Escapes string for HTML output. Specifically escapes '&', '<', '>', single and double quotes, '/'.

Template syntax

The basic tags use the JSP/ASP syntax, opening with <% and closing with %>.

In Vim, setting highlighting to "jsp" works well.

Interpolation

Expressions are between <%= and %> and the result of their evaluation is inserted in place.

<!-- This is template 'foo' -->
<h1><%= it.title %></h1>
// Invoke template 'foo'
var htmlSnippet = render.foo({ 'title': "Test" });

Evaluation

Statements are between <% and %> and are kept unchanged in the resulting function source code. There is no sandbox, so it is up to you to keep templates clean of malicious code.

<p>This is:
<% if (it.decision) { %>
truthy
<% } else { %>
falsy
<% } %>
</p>

Pre-processor directives

Pre-processing directives follow the general JSP/ASP syntax: <%@...%>. Whitespace is allowed before and after @ to suit your preference, making the following all equivalent:

<%@something arg1 arg2%>
<%@ something arg1 arg2 %>
<% @something arg1 arg2 %>
<% @ something arg1 arg2 %>

@define subName / @end

A template can contain sub-templates (a.k.a. "partials") which are private to the template in which they are defined. They are invoked as regular functions and do not need to be passed the it argument. If an object is passed as an argument, it is available within the sub-template as its to avoid conflicting with the template-wide it.

<p>content...</p>
<%@define foo %>
<p>This is sub-template foo, seeing: '<%= its.name %>'</p>
<%@end %>
<p>Let's use this sub-template a few times:</p>
<%= foo({name: 'first test'}) %>
<%= foo({name: 'second trial'}) %>
<p>That's it!</p>

@include

Includes the output of another template. For example if you have templates foo.jst and bar.jst, then in the former you could <%@include bar %>. This is a convenience shortcut however, so bar will inherit it as-is and cannot be passed further arguments.

MIT License

Copyright (c) 2016 Stephane Lavergne https://github.com/vphantom

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.