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

stencil-js

v0.8.4

Published

Async JavaScript Templating

Downloads

43

Readme

stencil

stencil is a templating engine designed by Andy VanWagoner (thetalecrafter) to enable templates to run in an environment with asynchronous I/O, such as node, as well as in the browser.

While there have been many other libraries with the same claims, all of the ones currently maintained use the mustache syntax, while this uses asp / erb / php style syntax.

Features

  • Async nested templates
  • Async tag to ensure template is processed sequentially
  • Use the same template code both server and client side

Shared templates

The motivator for stencil was to share templates between server and client code. The template can be used server side to generate a widget in the initial page load, and then the template can be included on the page to update the widget.

The code to generate and update a widget can be the same file.

Usage - template code

Templates are specified using php/asp syntax, with code inside special tags. By default the tags are php-style:

<? javascript code here ?>

There are also suffixes to the opening tag for ouput, include, and async blocks.

<?= 'Today is ' + (new Date()) // result is html encoded and included in output ?>
<?= 'hello', ' ', 'world' // multiple results can be output ?>
<?- '<em>Important</em>' // This output won't be encoded ?>

<?# 'child-template-id' // result passed as id to include() ?>
<?# { id:'child', async:'~' }, { custom:'data' } // override options and data variables in child template ?>

<?! setTimeout(next, 1000); // functionally equivalent to php sleep(1) ?>
<?! someAsyncFunction(param1, function whendone(result) {
		// do stuff with result
		print(result);
		next(); // continue processing the rest of the template
	}); ?>

Members of the data object are put in the scope of the template code:

<script type="text/template" id="template"><[CDATA[
	Why I don't teach English anymore:
	<?= message ?>.
]]></script>
<script>
	stencil({ id:'template' }, { message:'The book is not on the table' },
		function(err, result) {
			if (err) return console.log('The template failed to run.');
			console.log('The template result was:' + result);
		}
	);
</script>

Important note:

Unlike regular code tags, async tags cannot not include partial statements. All of the code inside will be wrapped into a function. All of the code following will also be wrapped into a function.

This would not work:

<?! if (true) { ?>some output<? } ?>

Since compiled it would be similar to:

(function(next){ if (true) { })(function() { print('some output'); } });

Usage - client side

<script src="stencil.js"></script>
<script type="text/template" id="dom_id">
	<[CDATA[
	... template code here ...
	]]>
</script>
<script>
	stencil.compile({ id:'dom_id' }, function(err, template) {
		template(
			{ data:object },
			function(data) { /* optional - use the data chunks */ },
			function(err, result) { /* all done */ },
		);
	});

	// or

	stencil('dom_id', { data:object }, function(err, result) {
		/* all done */ 
		if (err) { /* you broke it */ return; }
		/* use the result */
	});
</script>

Usage - server side

var stencil = require('./stencil');

stencil('/path/to/template', { data:object },
	function(data) { /* use the data chunks */ },
	function(err, result) { /* all done */ }
);

// same variations apply as client side.

Usage - custom tags

stencil({
	id:    id,
	start: '`',
	stop:  '`',
	echo:  'print',
	safe:  'encode',
	nest:  ' include this template:',
	async: '@'
}, data, function(err, result) {
	// here's my result
});

// template code:
My pet is `if (hungry) { `hungry` } else { `sleepy` }`.
His name is: `print pet.name`.
He looks like: ` include this template: 'looks_like', pet `.
`@my_async_function(function(result) { print(result); next(); });`
the end.

Usage - synchronous

You can fetch templates synchronously, if you need to. The caveat here is that you cannot do anything asynchronous in your template, or you will only get the ouput up until the first asynchronous part. The setting really only makes includes happen synchronously.

try { var result = stencil({ id:'id', sync_include:true }, data); }
catch (err) { /* You broke it. */ }

License

(The MIT License)

Copyright (c) 2012 Andy VanWagoner

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.