flatiron / plates

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

Plates 0.4.8 Issue with CharAt #90

Closed iwarner closed 11 years ago

iwarner commented 11 years ago

Hi

Receiving this

/vagrant/node_modules/plates/lib/plates.js:248 c = html.charAt(i);

with this code

    var match = { "taskList" : "New Value" };
    var output = Plates.bind(data, match);

data being a full HTML template

Appreciate the guidance

3rd-Eden commented 11 years ago

What is the actual issue? You didn't post the error message or even a stack trace.

LeytonC commented 11 years ago

I think i have had the same problem, i opened a html file using File System object, and when the readFile method called back with the data, the returned data is not explicitly a string object and of course chatAt method is not present. I fixed it by using the string() function when i made the call .bind to plates, this fixed the 'problem' (its not really a problem, it just needs the right type passed.)

Here is the callstack from node when you pass without a type cast to string.

<Loads of HTML string-like output, cut for the sake of space> has no method 'charAt' at Object.bind (/Users/leytonc/rss/node_modules/plates/lib/plates.js:255:18) at Object.bind (/Users/leytonc/rss/node_modules/plates/lib/plates.js:666:18)

i cut my functions names here since they will be pointless.

at fs.readFile (fs.js:LineNo:Place)
at Object.oncomplete (fs.js:297:15)

(Sorry if this is obvious, but i am not that experienced with node, an it looks like someone else had the same problem, hopefully it might help someone else.)

Southern commented 11 years ago

@LeytonC We need to see what you're trying to do in order to know what's going on. So far we just know that .bind isn't working because .charAt is throwing an error.

The only thing I can assume is that you're getting a buffer returned.

For example:

var html = '<span>This is valid code.</span>';

// This is what is expected.
var template = plates.bind(html);

// This is what's happening. It'll throw an error.
var template = plates.bind(new Buffer(html));

// This is what should be done to avoid the error.
var template = plates.bind((new Buffer(html)).toString());
LeytonC commented 11 years ago

That is the exact problem, when i read the documentation for file system, i did not realise that the data returned was a buffer. Please consider the example code below

// Example of problems with .chatAt
var plates = require('plates');
var fs = require('fs');

// Read in the file
fs.readFile('./example.html', function(err, data) {

    // What data type do we have?
    console.log('We have a type of: '+data.constructor.name);

    // and now we should have the problem
    plates.bind(data);  

});

the data is a buffer not a string, so you get the error.

3rd-Eden commented 11 years ago

You should specify a encoding when you fs.readFile for example fs.readFileSync('./example.html', 'utf-8') Not sure if this issue is worth fixing in Plates.

LeytonC commented 11 years ago

I added the encoding, and now i am getting a string back. Since this is technically not a plates issue, its a user issue there is nothing to fix, but its probably worth mentioning it somewhere in documentation or examples would be a good / better idea.

Southern commented 11 years ago

@LeytonC The fs module returning buffers is documented in Node. http://nodejs.org/api/fs.html#fs_fs_readfile_filename_encoding_callback