Skip to content

Javascript Markup (think Markaby for Javascript, something akin to Builder)

Notifications You must be signed in to change notification settings

smtlaissezfaire/jm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jm === “Javascript Markup”

jm is Javascript Markup. It’s a Markaby/Builder for Javascript.

Using

require jm:

var jm = require("jm");

If you are familiar with Builder in ruby, this will seem familiar to you:

jm.render(function(b) {
  b.div({'class': 'foo'}, function() {
    b.img({src: "foo.jpg"});
  });
});

// produces:

<div class="foo">
  <img src="foo.jpg" />
</div>

The real power here is that the data (templates) are really just code, and can be shared between both client and server side.

Examples

Self-closing tags:

jm.render(function(b) {
  b.img();
});
=> <img />

Tags with attributes:

jm.render(function(b) {
  b.div({id: 'bar'});
});
// => <div id='bar'></div>

Nested tags:

jm.render(function(b) {
  b.ul(function() {
    b.li(function() {
      b.text("hello");
    });
  });
});

// =>
  <ul>
    <li>hello</li>
  </ul>

Notice that the “text” method is a way to output raw text into the buffer. It’s an “escape hatch” of sorts.

A convenience provided is provided by jm in a situation like the one above:

b.li(function(b) {
  b.text("hello");
});

// => <li>hello</li>

can be more easily represented as:

b.li("hello");

// => <li>hello</li>

jm.render can also take an object/hash of locals:

jm.render({user_name: 'scott'}, function(b) {
  b.text(user_name);
});
// => "scott"

Partials:

Since templates are just functions, partials in scope can be rendered:

var partial = function(b) {
  b.p();
};

jm.render(function(b) {
  b.text(b.render(partial, {name: 'scott'}));
});

# => "<p>scott</p>"

The jm library provides a more idiomatic way, which involves registering the partial template:

jm.register("my_partial", function(b) {
  b.li(function() {
    b.text(name)
  });
});

jm.render(function(b) {
  b.render("my_partial", {name: 'scott'})
});

Bugs:

  • html5 attributes aren’t yet supported - this will be fixed in a future version.

  • At the present time, jm doesn’t perform w3c validation, as markaby can (optionally).

  • It suffers from the same performance constraints that markaby does, namely, that it doesn’t compile the templates. Whether or not this is a real performance bottle neck is still to be determined.

License

(The MIT License)

Copyright © 2010 Scott Taylor &lt;scott@railsnewbie.com&gt;

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Javascript Markup (think Markaby for Javascript, something akin to Builder)

Resources

Stars

Watchers

Forks

Packages

No packages published