Code Monkey home page Code Monkey logo

virtual-jade's Introduction

virtual-jade

NPM version Build status Test coverage

Compile your jade templates into Virtual DOM functions. Works with multiple Virtual DOM libraries, including:

For people who like declarative reactive templating, but don't like writing HTML or JSX.

Create a template:

.items
  each item in items
    .item(
      class={active: item.active}
      data-id=item.id
    )
      .item-title= item.title
      .item-description= item.description

require() your template as a function and use a rendering system like main-loop:

const mainLoop = require('main-loop');

const template = require('./items.jade');

const initState = {
  items: [],
};

const loop = mainLoop(initState, template, {
    create: require("virtual-dom/create-element"),
    diff: require("virtual-dom/diff"),
    patch: require("virtual-dom/patch"),
});
document.body.appendChild(loop.target);

Then update whenever you'd like!

loop.update({
  items: [
    {
      id: 'asdf',
      title: 'some title',
      description: 'some description',
      active: false,
    },
  ],
});

Notes

  • For easy configuration with Webpack, use virtual-jade-loader.
  • To translate with Babel, use babel-plugin-virtual-jade.
  • Can be used with any CommonJS environment with client-side require()s.
  • All templates must return a single root element.
  • Requires you to install the appropriate virtual-dom library in your top-level app.

API

fnStr = render(str, options)

str is the jade source as a string. fnStr is output JS that you should include as a CommonJS module.

Options are:

  • filename: path and name of Jade source file for str. Required if you use include or extends in templates.
  • marshalDataset=true: whether to convert data- attributes to dataset members. Set to false to treat as props with the same name as the attributes (if your target Virtual DOM renderer does not support the dataset API).
  • pretty=false: whether to beautify the resulting JS. Requires you to install js-beautify yourself.
  • propsWrapper: optional object to wrap Jade attributes in; for example, with propsWrapper = 'props', the template div(foo="bar") will translate to something like h('div', {props: {foo: 'bar'}}) rather than h('div', {foo: 'bar'})
  • rawProps: whether to skip Jade attribute -> HTML property conversion; this is set to true in the default Snabbdom configuration
  • serializeAttrsObjects: special behavior for the Snabbdom-style attrs attribute object. If true, object values within an attrs attribute will be automatically stringified (since HTML element attributes are always strings); for example, in div(attrs={foo: {hello: 'world'}}) the foo attr will end up in HTML as "{"hello":"world"}" (rather than "[object Object]").
  • runtime: optional override to include any arbitrary Virtual DOM library that defines the h() hyperscript function. E.g. var h = require('my-special-lib/h');
  • vdom: name of the Virtual DOM library configuration to load (currently either virtual-dom or snabbdom).

Returns a string that looks like:

function render(locals) {
  var result_of_with = /* stuff */
  if (result_of_with) return result_of_with.value;;
}

You are expected to eval() the string if you want the source as a function. Otherwise, just create a module in the following format:

const js = `module.exports = ${fnStr}`;

Within code blocks in your template code, you can access Jade mixin functions via the $mixins variable. In virtual-jade, mixins boil down to functions that take arguments and return a tree of h(name, attrs, children). They are like React stateless components. Accessing them via $mixins is useful for special cases where you want to pass around handles to blocks of Jade code as callback functions (see example below).

mixin item(x)
  .item
    .more-tree= x + 1

list-virtual-scroll(props={itemRenderer: $mixins.item})
// in list-virtual-scroll.jade
each val in allItems.slice(startIdx, endIdx)
  = props.itemRenderer(val)

Development notes

  • Install deps: npm install
  • Run tests: npm test
  • Run linter: npm run lint
  • Generate coverage report: npm run test-cov
  • Run all the verifications together: npm run test-ci
  • Run tests with verbose debugging output (compiled functions as well as rendered HTML): DEBUG=test npm test

virtual-jade's People

Contributors

andrewpurcell avatar dependabot[bot] avatar doublerebel avatar jonathanong avatar ryanhirsch avatar tdumitrescu 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

virtual-jade's Issues

TODO:

  • case
  • classes
  • caching
  • mixins
  • dependencies
  • interpolation
  • content
  • while loops
  • each
  • escape and unescaped

Sourcemap support in compiler

Currently when compiling with webpack, debugging an issue inside virtual-jade compilation is very hard.

The output is quite verbose. Source map support that maps to jade would be a time saver.

plugins

  • browserify
  • webpack
  • duo
  • component

"virtual-jade" does not support the "extends", "include" and other key words

Error: the "filename" option is required to use "extends" with "relative" paths
at Parser.resolvePath (D:\demows\koa-demo\node_modules\jade\lib\parser.js:486:13)
at Parser.parseExtends (D:\demows\koa-demo\node_modules\jade\lib\parser.js:505:21)
at Parser.parseExpr (D:\demows\koa-demo\node_modules\jade\lib\parser.js:221:21)
at Parser.parse (D:\demows\koa-demo\node_modules\jade\lib\parser.js:122:25)
at render (D:\demows\koa-demo\node_modules\virtual-jade\lib\index.js:43:25)
at DestroyableTransform.CompileJade as _transform
at DestroyableTransform.Transform._read (D:\demows\koa-demo\node_modules\through2\node_modules\readable-stream\lib_stream_transform.js:159:10)
at DestroyableTransform.Transform._write (D:\demows\koa-demo\node_modules\through2\node_modules\readable-stream\lib_stream_transform.js:147:83)
at doWrite (D:\demows\koa-demo\node_modules\through2\node_modules\readable-stream\lib_stream_writable.js:313:64)
at writeOrBuffer (D:\demows\koa-demo\node_modules\through2\node_modules\readable-stream\lib_stream_writable.js:302:5)

more compact results

  • don't bother rendering empty {}s or []s
  • don't bother wrapping expressions in [] if there is only 1 element
  • classes
    • don't bother .filter(Boolean) class arrays with only literals
    • concatenate compile-time them whenever possible

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.