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

breezeblock

v0.3.0

Published

A flexible JS-based templating engine

Downloads

4

Readme

BreezeBlock

An experimental templating language for JS that can compile directly to DOM.

Compatible with all modern browsers and IE9+.

npm install breezeblock --save

Using Breezeblock in Express:

var express = require('express');
var app = express();

app.engine('brz', require('breezeblock').renderTemplate);
app.set('view engine', 'brz');

app.get('/', function(req, res) {
    // This will render myTemplate.brz
    res.render('myTemplate', {foo: 'bar', my: 'variables'});
});

Using Breezeblock elsewhere:

var breezeblock = require('breezeblock');

// On the fly
breezeblock.renderTemplate(
    'templates/myTemplate.brz', // Path to template
    {my: 'variables'}, // Data to render template with
    function(err, output) {
        console.log(output);
    }
)

// Compiled
var compiledTemplate = breezeblock.compileTemplate('templates/myTemplate.brz');
console.log(compiledTemplate({my: 'variables'}));

Compiling your templates:

breeze-compile path/to/template.brz

Technique

BreezeBlock templates can compile to a number of different formats:

  • HTML, like most other templating engines
  • Precompiled HTML generator
  • Precompiled DOM generator

The HTML format simply outputs a string of markup when the template is rendered. The Precompiled HTML generator format outputs the template as JavaScript, preventing unnecessary overhead from parsing the template and traversing its AST. This feature is implemented in many templating engines. The DOM generator is unique: the template is output as JavaScript that directly generates DOM using createElement and createTextNode.

<!-- This: -->
<img src={{foo}}>

<!-- becomes this: -->
<script>
  function template(document, elem, scope) {
    var elem1 = document.createElement("img");

    elem1.setAttribute("src".toLowerCase(), scope["foo"]);
    elem.appendChild(elem1);
    elem.appendChild(document.createTextNode("\n"));
  }
</script>

Benefits

  1. Safety: Because innerHTML is never used and markup is never concatenated with user-provided data, there is virtually no risk of XSS.
  2. Performance: Because DOM is directly manipulated, there is no overhead of concatenating large strings or parsing the generated markup (if rendered on the client).
  3. Hygiene: Breezeblock gives you a great deal of freedom with your templates while preventing you from using many common template antipatterns, like dynamically naming nodes. This keeps templates simple and maintainable.
  4. Support: Using custom elements? Breezeblock should "just work". Using something custom? Pass your own document object to the DOM generation function.

Documentation

Write HTML as you normally would. Self-closing tags that are not void elements must use a closing slash (<foo />).

<div>
  Text nodes can be included inline
  <br>
  <!-- HTML comments are welcome -->
  <b class="really-bold">Mix and match tags</b>

  <my-custom-element />
</div>

Attribute names and values may be variable:

<div {{foo}}="bar"></div>
<div data-foo={{bar}}></div>

<!--
`fieldChecked` and `fieldReadonly` should contain "checked" and "readonly respectively"
-->
<input type="text" {{fieldChecked}} {{fieldReadonly}}>

<!-- Attributes may be conditional -->
<img src="foo.jpg" alt="" {% if imageTitle %}title={{imageTitle}}{%/ if %}>
<input type="checkbox" {% if checkboxChecked %}checked{%/ if %}>

Node contents can be variable:

<div>Here is my {{value}}</div>
<aside>{{value}}</aside>

Expressions can use dot notation and subscripts:

<img src={{images[index].src}} alt={{images[index]["alt"]}}>
<img src={{images[0].src}} alt={{images[0]["alt"]}}>

Work to Be Done

  • Decide which language constructs to make available
    • {% loop ... %}
    • ...
  • Figure out where each language construct should be usable
    • Conditional attribute values
    • Loops to define attributes
  • Support for "noescape" (triple brace {{{foo}}})