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

stache-backwards

v0.1.1

Published

This is an experiment in using a syntax like mustache / handlebars templates as a data storage format. (What I would call "reverse templating" -- but I see people already using that term to mean something else.) That is, instead of render content from an

Downloads

22

Readme

Intro

This is an experiment in using a syntax like mustache / handlebars templates as a data storage format. (What I would call "reverse templating" -- but I see people already using that term to mean something else.) That is, instead of render content from an object into a template, extract content out of a template-like document into a key-value hash object. Why this instead of something like JSON? Easier to read and edit, multiline, basically no escaping. And YAML? That was the closest existing thing I could think of, but I don't like that indentation is significant and I prefer a more straightforward way to indicate whether or not the content should be HTML-escaped.

"Data" storage really means content storage. The prototypical use case is to store multiple snippets of HTML (or similar content) in a file. For example, multiple chunks of content that correspond to the placeholders in a template representing a page can be stored in a single, easily readable and editable text file.

Example

Input:

{{{{content key="section_id"}}}}about{{{{/content}}}}

{{{{content key="head"}}}}
<link rel="stylesheet" href="about.css" media="all" />
{{{{/content}}}}

{{{{content key="primary"}}}}

<p>
This is some content.
</p>

{{{{/content}}}}

Output:

{
  section_id: 'about',
  head: '<link rel="stylesheet" href="about.css" media="all" />\n',
  primary: '\n<p>\nThis is some content.\n</p>\n\n'
}

Design

The original idea was to use a format like this:

Input:

{{a}}<p>{{/a}}
or
{{#a}}<p>{{/a}}

Output:

{
  a: '&lt;p&gt;'
}

Input:

{{{a}}}<p>{{{/a}}}
or
{{{#a}}}<p>{{{/a}}}

Output:

{
  a: '<p>'
}

But then I realized I could get Handlebars to do the heavy lifting of implementing this by using its raw block helper syntax:

Input:

{{{{content key="a"}}}}<p>{{{{/content}}}}

Output:

{
  a: '<p>'
}

Input:

{{{{content key="a" escape="html"}}}}<p>{{{{/content}}}}

Output:

{
  a: '&lt;p&gt;'
}

This even handles embedding Handlebars templates within the snippets. (With the caveat that Handlebars does not currently support nesting raw blocks. See wycats/handlebars.js#1056.) It's unfortunate that it requires so many {{{{ chars, and there may be undesirable performance implications, but it's great for a proof of concept.

Input

This will process input like the following:

{!--
  Sets a property named `a`.
  In other words, targetObject.a will equal "A".
--}
{{{{content key="a"}}}}A{{{/content}}}}

{!--
  Sets a property of `a` named `b`.
  In other words, targetObject.a.b will equal "B".
--}
{{{{content path="a.b"}}}}B{{{/content}}}}

{!--
  Will HTML-escape the value.
--}
{{{{content key="a" escape="html"}}}}A&B{{{/content}}}}

Usage

$ npm install stache-backwards
var
  stache_back = require('stache-backwards').handlebars,
  data;

data = stache_back.extract({template: input});

API

/**
 * Extracts content from a template.
 *
 * @param object opts Options
 * @param object|undefined opts.handlebars Instance of handlebars to use.
 * @param string|function opts.template Template to extract from. String or
 *   return value of handlebars.compile().
 * @param object|undefined opts.target Object to populate with content.
 * @param string|undefined opts.blockName Name of the handlebars block (helper).
 * @return object Hash of the content.
 */
handlebars.extract(opts)