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

stringtemplate-js

v0.1.1

Published

StringTemplate v4 for JavaScript

Downloads

639

Readme

StringTemplate v4 for JavaScript

stringtemplate-js is a pure JavaScript implementation of StringTemplate v4. Templates are compiled to JavaScript. The intent is to be fully compatible with StringTemplate syntax. This means that the same template processed with the same data should produce the same output when processed with the Java (reference implementation) and this JavaScript implementation. See below for known differences. The API will not be compatible with the Java implementation.

News

9-Aug

Version 0.1.1 ready for alpha testing. Feel free to use github Issues to report problems or make suggestions. Template regions not yet supported.

30-Jul

Apologies to anyone who downloaded the 0.1.0 version I added to npm. It does not work as is. It may work if you compile the pegjs grammer. I plan to update the version on npm soon.

26-Jul

Major milestone. StringTemplate-js now uses itself to generate compiled templates. It was able to pass all unit tests without using the Java version of StringTemplate for code generation!

What works at least a little

  • compile and process directory of template files (.st)
  • compile and process directory of raw template files (.st)
  • compile and process group file (.stg)
  • configurable start, stop delimiters and escaping delimiters
  • comments
  • dictionaries
  • literals string, true, false, lists
  • escapes such as $\t$ and $\\$
  • attribute expressions including implicit iteration over list/array attributes
  • property reference expressions
  • if, else if, else conditions including &&, ||, !
  • imports
  • template include expressions and argument passing by position or name and pass through (...)
  • expression options
  • sub templates
  • map expressions
  • rotating through templates while mapping
  • function expressions
  • auto indent writer
  • indirect property reference
  • indirect template include
  • zipping multiple lists/arrays while mapping

What doesn't work or isn't tested

  • regions

Nest step is to stabilize, and release first version without support for regions so other can try it out. Then come back and implement regions.

License

BSD

Install

StringTemplate adds two command line utilities. The template compiler stc and standalone tool stst for testing templates with JSON input. For that reason it is best to install globally.

npm install -g stringtemplate-js

If you don't install globally links to the stc and stst commands are in the node_modules/.bin folder.

Using

The file samples/hello/hello.st contains (with comments removed):

hello(audience) ::= <<Hello $audience;null="is anyone there?"$!

>>
  • Step 1 compile the templates using the stc command. For example:
cd samples/hello
stc hello.st

This produces hello_stg.js in the same folder. Type stc -h for help on stc command line options.

Templates can also be compiled from your own application using the API in compiler/stc.js. Grunt task TBD.

  • Step 2 Execute the compiled template. This can be done with stst that is in the bin folder.
cd samples/hello
stst hello hello.json

The first argument is the template name without the _stg.js suffix. The second argument is a JSON file that supplies the data for the template. Type stst -h for help on stst command line options.

To execute the template from your application add code similar to the following:

cd samples/hello
node
var st = require("stringtemplate-js");
var g = st.loadGroup(require("./hello_stg"));
console.log(g.render("hello", ["world"]);
// or
console.log(g.render("hello", {audience:"world"});

API

tbd

Building

Additional development dependencies: need grunt-cli, mocha and pegjs installed globally

Known Differences

This section lists differences between this implementation and the Java reference implementation

  • The Java AutoIndentWriter will strip lone cr (\r) from the output. This autoIndentWriter normalizes lone cr to the OS new line. A lone cr may be unlikely. One way to get one is with $"\r"$.

  • The Java implementation allows any start or stop delimiter even if they will later cause confusion. This implementation restricts the delimiters to "#$%^&*<>"

  • The JavaScript implementation doesn't support the old v3 style group file header

  • The order in which object properties (or map/dictionary keys) are iterated over may be different. For example $obj:T()$ when obj is { "a": "foo", "b": "bar"} could call template T with ["a", "b"] or ["b", "a"]. The JavaScript implementation will iterate over object properties in the order returned by Object.keys(obj);