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

wkc-react-jade

v2.5.0-x

Published

Compile Jade to React JavaScript

Downloads

6

Readme

react-jade

Compile Jade to React JavaScript

Build Status Dependency Status NPM version

Installation

npm install react-jade

Usage

With Browserify

If you are using browserify, just write a file that looks like the following, then use react-jade as a transform. It will then inline the result of calling jade.compileFile automatically.

var React = require('react');
var jade = require('react-jade');

var template = jade.compileFile(__dirname + '/template.jade');

React.render(template({local: 'values'}), document.getElementById('container'));
browserify index.js --transform react-jade > bundle.js

Without Browserify

If you are not using browserify, you could manually compile the jade to some client file. e.g.

var fs = require('fs');
var jade = require('react-jade');

fs.writeFileSync(__dirname + '/template.js', 'var template = ' + jade.compileFileClient(__dirname + '/template.jade'));

Then on your html page:

<div id="container"></div>
<script src="http://fb.me/react-0.12.2.js"></script>
<script src="template.js"></script>
<script>
  React.render(template({local: 'values'}), document.getElementById('container'));
</script>

Server Side

You can also use react-jade to render templates on the server side via React.renderToString. This is especially useful for building isomorphic applications (i.e. applications that run the same on the server side and client side).

var fs = require('fs');
var React = require('react');
var jade = require('react-jade');

var template = jade.compileFile(__dirname + '/template.jade');

var html = React.renderToString(template({local: 'values'}));
fs.writeFileSync(__dirname + '/template.html', html);

ES6

If you are using ES6 server side, or the browserify transform client side (even without any other ES6 support), you can use Tagged Literals to embed your jade code directly within your JavaScript components:

var TodoList = React.createClass({
  render: jade`
ul
  each item in this.props.items
    li= item
`
});
var TodoApp = React.createClass({
  getInitialState: function() {
    return {items: [], text: ''};
  },
  onChange: function(e) {
    this.setState({text: e.target.value});
  },
  handleSubmit: function(e) {
    e.preventDefault();
    var nextItems = this.state.items.concat([this.state.text]);
    var nextText = '';
    this.setState({items: nextItems, text: nextText});
  },
  render: jade`
h3 TODO
TodoList(items=this.state.items)
form(onSubmit=this.handleSubmit)
  input(onChange=this.onChange value=this.state.text)
  button= 'Add #' + (this.state.items.length + 1)
`.locals({TodoList: TodoList})
});
React.render(TodoApp(), mountNode);

API

var jade = require('react-jade');

jade(options) / jade(file)

Acts as a browserify transform to inline calls to jade.compileFile. The source code looks something like:

function browserify(options) {
  function transform(file) {
    return new TransformStream(); //stream to do the transform implemented here
  }
  if (typeof options === 'string') {
    var file = options;
    options = arguments[2] || {};
    return transform(file);
  } else {
    return transform;
  }
}

jade.compileFile(filename, options) => fn

Compile a jade file into a function that takes locals and returns a React DOM node.

jade.compileFileClient(filename, options)

Compile a jade file into the source code for a function that takes locals and returns a React DOM node. The result requires either a global 'React' variable, or the ability to require 'React' as a CommonJS module.

jade.compile(jadeString, options) => fn

Same as jade.compileFile except you pass an inline jade string instead of a filename. You should set options.filename manually.

jade.compileClient(jadeString, options)

Same as jade.compileFileClient except you pass an inline jade string instead of a filename. You should set options.filename manually.

template.locals(locals)

You can set default locals values via the template.locals api. e.g.

var React = require('react');
var jade = require('react-jade');

var template = jade.compileFile(__dirname + '/template.jade').locals({title: 'React Jade'});

React.render(template({local: 'values'}), document.getElementById('container'));

Differences from jade

React Jade has a few bonus features, that are not part of Jade.

Automatic partial application of on functions

In react, you add event listeners by setting attributes, e.g. onClick. For example:

button(onClick=clicked) Click Me!
var fn = jade.compileFile('template.jade');
React.render(fn({clicked: function () { alert('clicked'); }), container);

Often, you may want to partially apply a function, e.g.

input(value=view.text onChange=view.setProperty.bind(view, 'text'))
function View() {
}
View.prototype.setProperty = function (name, e) {
  this[name] = e.target.value;
  render();
};
var view = new View();
function render() {
  React.renderComponent(fn({view: view}), container);
}

Because you so often want that .bind syntax, and it gets pretty long and cumbersome to write, react-jade lets you omit it:

input(value=view.text onChange=view.setProperty('text'))

This is then automatically re-written to do the .bind for you.

Style

In keeping with React, the style attribute should be an object, not a string. e.g.

div(style={background: 'blue'})

valueLink

The valueLink property supports two way binding to this.state.name by default. e.g.

input(valueLink=this.state.name)

is eqivalent to:

input(valueLink={value: this.state.name,requestChange:function(v){ this.setState({name:v})}.bind(this)})

This makes building simple forms where each input should use a property of the state very easy.

Unsupported Features

Although a lot of jade just works, there are still some features that have yet to be implemented. Here is a list of known missing features, in order of priority for adding them. Pull requests welcome:

  • mixins
  • attribute extension/merging (via &attributes)
  • case/when
  • using each to iterate over keys of an object (rather than over items in an array)
  • interpolation
  • attribute interpollation
  • special handling of data-attributes
  • outputting unescaped html results in an extra wrapper div and doesn't work for attributes

License

MIT