Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue when precompile handlebars template using AMD pattern work with requirejs #528

Closed
xujihui1985 opened this issue May 18, 2013 · 7 comments
Labels
Milestone

Comments

@xujihui1985
Copy link

when precompile several templates into my template.js with amd pattern, the compiled js seems not correct.
I was using the latest Handlebars.cmd to compile the templates.

command: handlebars .\templates -f .\public\scripts\app\templates.js -a -n Handlebars.tpl
it just return the first template so I can't get rest template. I need to manually remove the return to workaround

define(['handlebars'], function(Handlebars) {
  var template = Handlebars.template, templates = Handlebars.tpl = Handlebars.tpl || {};

// can't return
return templates['post'] = template(function (Handlebars,depth0,helpers,partials,data) {
  this.compilerInfo = [3,'>= 1.0.0-rc.4'];
helpers = helpers || Handlebars.helpers; data = data || {};
  var buffer = "", stack1, functionType="function", escapeExpression=this.escapeExpression;


  buffer += "<div class='post'>\r\n    <h1>By ";
  if (stack1 = helpers.auther) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
  else { stack1 = depth0.auther; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
  buffer += escapeExpression(stack1)
    + "</h1>\r\n\r\n    <div class=\"body\">";
  if (stack1 = helpers.content) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
  else { stack1 = depth0.content; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
  buffer += escapeExpression(stack1)
    + "</div>\r\n\r\n</div>";
  return buffer;
  });

//second
return templates['postDate'] = template(function (Handlebars,depth0,helpers,partials,data) {
  this.compilerInfo = [3,'>= 1.0.0-rc.4'];
helpers = helpers || Handlebars.helpers; data = data || {};
  var buffer = "", stack1, functionType="function", escapeExpression=this.escapeExpression;


  buffer += "<div>\r\n    <span class=\"month\">";
  if (stack1 = helpers.month) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
  else { stack1 = depth0.month; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
  buffer += escapeExpression(stack1)
    + "\r\n        <span class=\"year\">";
  if (stack1 = helpers.year) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
  else { stack1 = depth0.year; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
  buffer += escapeExpression(stack1)
    + "</span>\r\n    </span>\r\n    <span class=\"day\">";
  if (stack1 = helpers.day) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
  else { stack1 = depth0.day; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
  buffer += escapeExpression(stack1)
    + "</span>\r\n</div>";
  return buffer;
  });
});
@kpdecker
Copy link
Collaborator

We need to update the bin script to look at the number of templates involved, not the number of arguments.

@xujihui1985
Copy link
Author

yes, I found the reason that because the target I try to compile is a directory

 if (argv.simple) {
      output.push(handlebars.precompile(data, options) + '\n');
    } else if (argv.partial) {
      if(argv.amd && argv._.length == 1){ output.push('return '); }
      output.push('Handlebars.partials[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n');
    } else {
      console.dir(argv);
      if(argv.amd && argv._.length == 1){ output.push('return '); }
      output.push('templates[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n');
    }

the argv._ is still 1, I think what the author try to archieve is that when there is only one file, then return the templates, if the file is greater than 1, then didn't return, I think we could add a counter when read the file from the directory, like you said , look at the number of templates involved, not the number of arguments

@xujihui1985
Copy link
Author

I'm trying to fix that on my local machine, and will ask for a pull request after I fix that

@kpdecker
Copy link
Collaborator

I believe this is the same issue as #517

@xujihui1985
Copy link
Author

Hi kpdecker,
I extract a method to get the file count before processTemplate and to test the count of the files instead of the count of the input template, and it's work for my case. please help to review the changes

var files = [];
getFiles(template, root,files);
argv.fileCount = files.length;

function getFiles(template,root,result){
  var stat = fs.statSync(template);
  if(stat.isDirectory()){
    fs.readdirSync(template).map(function(file){
      var path = template + '/'+file;
      if(extension.test(path) || fs.statSync(path).isDirectory()){
         getFiles(path, root || template,result);
      }
    })
  }else{
    result.push(template);
  }
}

 if (argv.simple) {
      output.push(handlebars.precompile(data, options) + '\n');
    } else if (argv.partial) {
      if(argv.amd && argv.fileCount.length == 1){ output.push('return '); }
      output.push('Handlebars.partials[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n');
    } else {
      //console.dir(argv);
      if(argv.amd && argv.fileCount.length == 1){ output.push('return '); }
      output.push('templates[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n');
    }

@kpdecker
Copy link
Collaborator

Can you open a pull request and we can review it there?

@kpdecker
Copy link
Collaborator

Fixed by #533

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants