0.2.78 • Published 7 years ago

jangular v0.2.78

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

Jangular

Join the chat at https://gitter.im/gethuman/jangular

This library can be used to render Angular 1.x templates on the server or command line.

Note that this library is in beta and should not be used in production...yet. Pull requests are welcome!

Command Line Usage

The best way to play around with Jangular is to use the command line tool. First install Jangular globally:

npm install jangular -g

Then try out some of these commands to get started:

jangular -t '<div>{{ foo }}</div>' -d '{ "foo": "hello, world" }'

The output should be:

<div>hello, world</div>

Instead of passing in the template and data, you can instead just use file paths. The following will only work if these files actually exist on your local system:

jangular -t somedir/blah.html -d otherdir/foo.json

Note that for the data file, you can use either a JSON file or a Node JavaScript file that has a module.exports.

Command Line Options

  • -t or --tpl - These is your Angular template. Either pass in the template html or a path to your template
  • -d or --data - The scope to use when you start evaluating. Either JSON or a path to a JSON or JS file
  • -s or --strip - When you include this option, all Angular binding directives will be stripped out of template after evaluating.

The reason why strip is in there is that sometime you want the server side to produce HTML without any Angular binding directives so that when Angular bootstraps, it does not prematurally modify the rendered HTML.

Code Usage

From the command line enter:

npm install jangular --save

In your Node.js code, you first need to require jangular:

var jangular = require('jangular');

The first thing you want to do is convert your HTML file to an object. Here is one example of doing this that you can adapt to meet your needs.

var fs = require('fs');
var html = fs.readFileSync(__dirname + '/something.html');

// either pass in callback
jangular.htmlToObject(html, function (err, jangularDom) {
    // use jangularDom (see below)
});

// or you can use promises
jangular.htmlToObject(html)
    .then(function (jangularDom) {
        // use jangularDom (see below)
    })
    .catch(function (err) {
        // do something here for error
    });

As an alternative, you can write your HTML with Jyt in which case you use the Jyt function output.

// add all the Jyt HTML functions to the current node.js module scope
jangular.addShortcutsToScope(global);

var jangularDom = div({ 'ng-if': 'someVal', 'class': 'foo' }, span({ 'ng-bind': 'greeting' }, 'hello'));

Now that you have the fake DOM object used by Jangular, we can pass that into the render function.

var data = { someVal: true, greeting: 'Hello, world' };
var html = jangular.render(jangularDom, data);

Now you have the rendered HTML.

How it Works

The input into the main render() function for this library is an Angular template and a JSON object that contains the scope data used to render the template. The render() function has the following steps:

  1. Translate template from HTML to an object. This object is a tree structure that represents the DOM.
  2. Traverse the tree and for each node/element:
    1. Use $parse from Angular core to evaluate expressions
    2. Run the link() method of any directives on the element (more on this below)
  3. After traversal complete and transforms done, translate the DOM-like object to an HTML string.

Directives

This library includes support for many of the Angular core directives but if you have a custom directive you want to have evaluated, you register it with Jangular like this:

jangular.addDirective('my-foo', function transform(scope, element, attrs) {
    // you can either modify the element here or return a band new element
});

It is up to you to define your own server side "directives". The way I suggest using this is to come up with a vanilla JavaScript object format that represents once of your "components" then write generic code to take your custom component objects and either transpile them into directives on the client side or into these jangular.addDirective() calls on the server side. An example of how we do something like this at GetHuman use this is in our pancakes-angular library. Just note that the pancakes-angular library is much more unstable than this library so you may want to just see how that works and create your own version.

Once you have registered a directive, you can reference it as an attribute like this:

var template = '<div my-foo></div>

Filters

You can add filters that are used as your template is rendered by doing:

jangular.addFilters([
    myFilter: function (array) {}
])

Known Issues

One thing is that if you use the {{ }} expression syntax, Jangular will replace the expression with the value. This may cause an issue if you are having the Angular client take over because Angular will just see the evaluated value and not the expression. For this reason, for now, we strongly suggest using ng-bind instead of {{ }} within inner HTML.

Use Cases

The primary use case for Jangular is when you are working on a consumer facing app where visitors come from external links, search results and/or ads. In that case, the initial page load performance has a big impact on bounce rate (the rate at which users leave your site after one page view) and thus your search ranking.

Pancakes

This library can be used by itself to do simple Angular rendering, but if you are interested in how this can be used for a full app, check out pancakes.js and the pancakes-angular plugin plugin. To see how pancakes-angular is used, check out the sample pancakes project. Just note that the pancakes libraries are extremely opinionated and have not been tested with anything outside of GetHuman.

Contributing

Working on this library is pretty straightforward.

git clone git://github.com/gethuman/jangular.git
npm install
gulp test --cov=true

Here are some of the things on the ToDo list:

  • Update to Angular 1.4 $parse
  • An easier way to register directives
  • Directive transclusion (requires modifications to traversal algorithm)
  • Angular2 branch
0.2.78

7 years ago

0.2.77

7 years ago

0.2.76

7 years ago

0.2.75

7 years ago

0.2.74

7 years ago

0.2.73

8 years ago

0.2.72

8 years ago

0.2.71

8 years ago

0.2.70

8 years ago

0.2.69

8 years ago

0.2.68

8 years ago

0.2.67

8 years ago

0.2.66

8 years ago

0.2.65

8 years ago

0.2.64

8 years ago

0.2.63

8 years ago

0.2.62

8 years ago

0.2.61

8 years ago

0.2.60

8 years ago

0.2.59

8 years ago

0.2.58

8 years ago

0.2.57

8 years ago

0.2.56

8 years ago

0.2.55

8 years ago

0.2.54

8 years ago

0.2.53

8 years ago

0.2.52

8 years ago

0.2.51

8 years ago

0.2.50

8 years ago

0.2.49

8 years ago

0.2.48

8 years ago

0.2.47

8 years ago

0.2.46

8 years ago

0.2.45

8 years ago

0.2.44

8 years ago

0.2.43

9 years ago

0.2.42

9 years ago

0.2.41

9 years ago

0.2.40

9 years ago

0.2.39

9 years ago

0.2.38

9 years ago

0.2.37

9 years ago

0.2.35

9 years ago

0.2.33

9 years ago

0.2.32

9 years ago

0.2.30

9 years ago

0.2.29

9 years ago

0.2.28

9 years ago

0.2.26

9 years ago

0.2.25

9 years ago

0.2.24

9 years ago

0.2.23

9 years ago

0.2.22

9 years ago

0.2.21

9 years ago

0.2.20

9 years ago

0.2.19

9 years ago

0.2.18

9 years ago

0.2.17

9 years ago

0.2.16

9 years ago

0.2.15

9 years ago

0.2.14

9 years ago

0.2.13

9 years ago

0.2.12

9 years ago

0.2.11

9 years ago

0.2.10

9 years ago

0.2.9

9 years ago

0.2.8

9 years ago

0.2.7

9 years ago

0.2.6

9 years ago

0.2.5

9 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.59

9 years ago

0.1.58

9 years ago

0.1.57

9 years ago

0.1.56

9 years ago

0.1.55

9 years ago

0.1.54

9 years ago

0.1.53

9 years ago

0.1.52

9 years ago

0.1.51

9 years ago

0.1.50

9 years ago

0.1.49

9 years ago

0.1.48

9 years ago

0.1.47

9 years ago

0.1.46

9 years ago

0.1.45

9 years ago

0.1.44

9 years ago

0.1.43

9 years ago

0.1.42

9 years ago

0.1.40

9 years ago

0.1.31

9 years ago

0.1.30

9 years ago

0.1.29

9 years ago

0.1.26

9 years ago

0.1.25

9 years ago

0.1.20

9 years ago

0.1.12

10 years ago

0.1.11

10 years ago

0.1.10

10 years ago

0.1.6

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago

0.0.1

10 years ago