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

qejs

v3.0.5

Published

Asyncronous Embedded JavaScript Templates with Q

Downloads

769

Readme

Asyncronous Embedded JavaScript Templates with Q

This library impliments Embedded JavaScript with some asyncronous additions provided by Q. It is broadly based on https://github.com/visionmedia/ejs and fully supports everything except filters. For a full discussion of the reasoning behind this, see Features below. The promises implimentation for ayncronous operations uses https://github.com/kriskowal/q so you'll need to make sure that all asyncronous functions/values you supply to the templates are 'thenables' so that they'll work with Q. All that means in practice is that you need your promises to have a function then that you can call with a promise.

Installation

$ npm install qejs

Example

<% if (user) { %>
    <h2><%= user.getNameAsync() %></h2>
<% } %>

Usage

qejs.compile(str, options);
// => Function

qejs.render(str, options);
// => promise

qejs.render(str, options).then(function(output){
        //output is a string
    });

Options

  • cache Compiled functions are cached, requires filename
  • filename Used by cache to key caches
  • scope Function execution context
  • debug Output generated function body
  • open Open tag, defaulting to "<%"
  • close Closing tag, defaulting to "%>"
    •             All others are template-local variables

Custom tags

Custom tags can also be applied globally:

var qejs = require('qejs');
qejs.open = '{{';
qejs.close = '}}';

Which would make the following a valid template:

<h1>{{= title }}</h1>

Unbuffered Code

<% code %>

QEJS supports exactly the same syntax as EJS for unbuffered code, usefull for conditionals, loops etc.

Escapes HTML

<%= code %>

This differs from EJS in that if code returns a promise, it is resolved and then escaped before being outputted. While this is happening, QEJS will continue on to render the rest of the template, allowing many promised functions to be executed in parallel.

Unescapted Buffering

<%- code %>

If code isn't a promise, this will work exactly like EJS, but if code is a promise, we will resolve it, before outputting the resolved value. We won't do any escaping on this value, so only use for trusted values, not user input.

Async Blocks

<% promise -> value %>...Use value here...<% < %>
<% PromiseForAnArray -> [valueA, valueB, valueC...] %>...Use values individually here...<% < %>
<% [promiseA,promiseB,promiseC...] -> values %>...Use identifier as an array of resolved values...<% < %>
<% [promiseA,promiseB,promiseC...] -> [valueA,valueB,valueC...] %>...Use values individually here...<% < %>

Async blocks are considered a relatively advanced feature, and where possible you should try and tick to just returning promises through <%= code %> as it's much easier to write that without creating bugs.

Having said that, async blocks are not difficult to write and I hope you'll end up really loving them for those times when you really need them.

What happens in an async block is we reserve a space for whatever text is outputted by the block, allowing you to use any QEJS inside the async block (including another async block). We then resolve the value of the promise you give us, and we give it the name you specify on the right hand side of the arrow operator. The async block starter must go in its own separate unbuffered code block, but you could put other things like comments inside the block with the end marker.

Once you go past the end marker of an async block, you will no longer have access to the value of the promise. This lets us run lots of calls in parallel, and menas that you can use async blocks inside if statments, for statements, while statements, functions statements, pretty much anywhere you could write regular EJS.

Newlien slurping

if you end any code block with a - even if it's an async block, we'll support newline slurping for you (<% code -%> or <% -%> or <%= code -%>, <%- code -%>, <% promise -> result -%> or <% < -%>) That is to say, we won't output the next new line after we see that symbol. QEJS never outputs a newline inside a code block.

Filters

QEJS doesn't support filters. Although in a way it seems a shame not to maintain 100% compatability with the syncronous form of EJS, I think that it's more important to keep this library lean. Filters don't really add a lot, you can always attach such methods to arrays yourself and use standard javascript syntax, I don't want to hurt the purity of EJS though, so I've chosen not to add this syntax (bloat).

ExpressJS Integration

I'm really keen to get express integration working, however unfortunately at version 2.x this isn't possible because of our asyncronous rendering, Shims can be made. However I will endevour to ensure this does support Express.js @ v3.0 as soon as possible after that's released.

License

(The MIT License)

Copyright (c) 2012 Forbes Lindesay <[email protected]>

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.

EJS License

The EJS License (by visionmedia) on which this library is based, is reproduced here:

(The MIT License)

Copyright (c) 2009-2010 TJ Holowaychuk <[email protected]>

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.