flatiron / plates

Light-weight, logic-less, DSL-free, templates for all javascript environments!
MIT License
831 stars 69 forks source link

README's "Simple Example" is not working. Is Plates broke, or is this a bug? #25

Closed leeola closed 12 years ago

leeola commented 12 years ago

With a completely fresh project, running "npm install plates", and then running the example code of..

var Plates = require('plates');

var html = '<div id="test">Old Value</div>';
var data = { "test": "New Value" };

var output = Plates.bind(html, data);

The following error is given:

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
TypeError: Object #<Object> has no method 'bind'
    at Object.<anonymous> (/mnt/ws/users/leeolayvar/136705/test.js:6:21)
    at Module._compile (module.js:411:26)
    at Object..js (module.js:417:10)
    at Module.load (module.js:343:31)
    at Function._load (module.js:302:12)
    at Array.<anonymous> (module.js:430:10)
    at EventEmitter._tickCallback (node.js:126:26)

Thoughts? Note that Plates also does not have the Map attribute either.

leeola commented 12 years ago

Looks like this happened between npm versions 0.2.2 and 0.4.0. 0.2.2 works, 0.4.0 doesn't.

leeola commented 12 years ago

Found the issue.

(typeof process !== 'undefined' && ~process.title.indexOf('node')) ? exports : {};

If your process does not have a title, Plates does not properly write to exports as it should.

Link: https://github.com/flatiron/plates/blob/e91463b65f5cd63accda77463852c7fca2c29814/lib/plates.js#L2

Is there a reason to not simply check the existence of exports?

;var Plates = (typeof exports !== 'undefined') ? exports : {};
heapwolf commented 12 years ago

@leeolayvar so a global named exports can easily exist in the browser. However an object named process as well as it having a member named title is a lot less likely. However, the formula for this should actually be...

typeof process !== 'undefined' && process.title) ? exports : {}

The problem with the way it was...

typeof process !== 'undefined' && ~process.title.indexOf('node')) ? exports : {};

Is that you're node.js process can be re-titled either by the user or sometimes by node.js (for instance when using the cluster module)

But you're process object will always have a title. If you remove it, you'll break stuff.

heapwolf commented 12 years ago

Fixed in 96bccdd937c3ca579ea82cb882270b603d61430f and tagged at v0.4.4

leeola commented 12 years ago

Yea, I was discussing this with the folks in #nodejitsu. My question is, on Cloud9 (an IDE for JS/etc), process.title actually equals "". So this issue will still exist, since process will not be undefined, but process.title will be an empty string (and i believe return false, correct?)

.. hopefully this makes sense, i just woke up. heh

heapwolf commented 12 years ago

Edit: I read your question completely in reverse. I just woke up as well.

heapwolf commented 12 years ago

I was suffering from a momentary lapse of well known knowledge on account of even needing to think about it. Because for some reason Cloud9 does something special to the process title, the forumla will need to be...

;var Plates = (typeof process !== 'undefined' && typeof process.title !== 'undefined') ? exports : {};

The fix is committed.

leeola commented 12 years ago

Looks great :D

Yea, my discussion on IRC was simply trying to find who actually had the bug. Plates for expecting the title to exist, or Cloud9 for not having a title.

heapwolf commented 12 years ago

i recommend vim ;)

leeola commented 12 years ago

I actually switched to C9 from Vim :)

I prefer Vim also, but i'm trying out C9 out of curiosity. Plus, all of Vim tools were Python-centric.. JS is new territory to me :s. IDEs in the browser is nifty, even if not as fully featured as Vim yet.

heapwolf commented 12 years ago

i love vim ;) you should check out NERDTree (http://www.vim.org/scripts/script.php?script_id=1658) and powerline (https://github.com/Lokaltog/vim-powerline). they make vim sexy.