Coder Social home page Coder Social logo

hyperstache's Introduction

Hyperstache

Build Status Badge size codecov code style: prettier

Logic-less templates to template literals transformer.
Hyperstache includes a full parser and runtime.
It uses no eval and minimal regex for the best performance.
It's largely compatible with Handlebars and Mustache templates.

npm: npm install hyperstache --save
cdn: https://unpkg.com/hyperstache
module: https://unpkg.com/hyperstache?module

Why?

The goal is to make projects invested in Handlebars templates adopt a tagged templates only solution easily or add an additional layer of logic-less templates on top of any tagged template library.

hyperstache by the numbers:

๐Ÿš™ 2.07kB when used directly in the browser

๐Ÿ 1.74kB hyperstache/mini version (built-in helpers)

๐ŸŽ 1.07kB if compiled using babel-plugin-hyperstache

Features

  • variables {{escaped}}, {{{unescaped}}}
  • variables dot notation {{obj.prop}}
  • helpers {{loud lastname}}
  • helpers literal arguments: numbers, strings, true, false, null and undefined
  • basic block helpers {{#bold}}
  • built-in helpers: if, unless, each, with
  • helper hash arguments
  • comments {{!comment}}, {{!-- comment with }} --}}
  • whitespace control {{~ trimStart }}
  • helper block parameters
  • subexpressions
  • partials {{>partial}}

Usage (CodeSandbox)

import { compile } from "hyperstache";

const o = (...args) => args;
const template = compile.bind(o)`
  <p>
    Hello, my name is {{name}}. 
    I am from {{hometown}}. I have {{kids.length}} kids:
  </p>
  <ul>
    {{#each kids}}
      <li>{{name}} is {{age}}</li>
    {{/kids}}
  </ul>
`;

const data = {
  name: "Alan",
  hometown: "Somewhere, TX",
  kids: [{ name: "Jimmy", age: "12" }, { name: "Sally", age: "4" }]
};
console.log(template(data));

/** =>
[
  [
    "<p>โ†ต    Hello, my name is ",
    ". โ†ต    I am from ",
    ". I have ",
    " kids:โ†ต  </p>โ†ต  <ul>",
    "</ul>"
  ],
  "Alan",
  "Somewhere, TX",
  2,
  [
    ["", "", ""],
    [
      ["<li>", " is ", "</li>"],
      "Jimmy",
      "12"
    ],
    [
      ["<li>", " is ", "</li>"],
      "Sally",
      "4"
    ]
  ]
]
 */

API

compile(statics, ...exprs)

registerHelper(name, fn)

escapeExpression(str)

new SafeString(htmlStr)

Real world (CodeSandbox)

import { html } from "sinuous";
import { compile } from "hyperstache";

const template = compile.bind(html)`
  <p>
    Hello, my name is {{name}}. 
    I am from {{hometown}}. I have {{kids.length}} kids:
  </p>
  <ul>
    {{#each kids}}
      <li>{{name}} is {{age}}</li>
    {{/kids}}
  </ul>
`;

const data = {
  name: "Alan",
  hometown: "Somewhere, TX",
  kids: [{ name: "Jimmy", age: "12" }, { name: "Sally", age: "4" }]
};
const dom = template(data);
document.body.append(dom);

hyperstache's People

Contributors

alexgustafsson avatar dependabot[bot] avatar luwes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hyperstache's Issues

Working example of integration with Sinuous?

Do you have a working example of how to integrate this with Sinuous?

Specifically, how to use observable to make templates that can update?

Can you make {{#each}} work with map to support lists in the data-model, or how would that work?

Or is this designed to just render once and not update? (if so, what's the point of integrating with Sinuous?)

Custom tags?

Is it possible to use custom tags โ€” e.g., [[ ]] instead of {{ }} โ€” with this library? If so, how?

Add mounted hook/callback

At particular parts of compilation, could we add the ability to hook into the program? This could be useful for creating plugins.

Possible hooks:
Mounted
Compiled
Iteration lifecycles

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.