sstephenson / eco

Embedded CoffeeScript templates
MIT License
1.71k stars 70 forks source link

Any intention to add an 'include' tag to enable nested templates? #42

Open stringfellow opened 12 years ago

stringfellow commented 12 years ago

I've got a couple of cases where I have a template that I reuse within other templates and at the moment its a copy-paste job. Am I missing something? Is this a reasonable feature request?

sircambridge commented 12 years ago

will this work for you ?

<%- eco.render(fs.readFileSync('someTemplate.eco'), @ ) %>

stringfellow commented 12 years ago

Hmm, possibly. I'm using eco in spinejs and I don't think I have implicit access to the eco module within the template... But am probably missing something.

sircambridge commented 12 years ago

You might be able to global.eco = require('eco') ?

Or pass the Eco reference into the context object, @eco = require('eco')

Gene Tsai Sent with Sparrow (http://www.sparrowmailapp.com/?sig)

On Monday, March 19, 2012 at 3:33 AM, Steve Pike wrote:

Hmm, possibly. I'm using eco in spinejs and I don't think I have implicit access to the eco module within the template... But am probably missing something.


Reply to this email directly or view it on GitHub: https://github.com/sstephenson/eco/issues/42#issuecomment-4570772

trans commented 12 years ago

I am compiling eco template using eco command-line and wondering the same thing -- how to nest templates?

lagartoflojo commented 12 years ago

In my case, I'm sending the templates directly to the clients, and they all get stored in the "JST" variable (this is done automatically by Rails).

To nest a template then, I use:

<%- JST['template_name'](myVar: 1) %>

timaschew commented 11 years ago

@sircambridge I try this: pass to the outer template the eco, fs, and templateDir object, in the template I do this:

<%- @eco.render( @fs.readFileSync(@templateDir + '/Makefile_partial.eco'), @) %>

But I get an error, whatever the partial contains some string, nochting (empty) or a variable print:

Users/timaschew/project/node_modules/eco/lib/scanner.js:31
      this.source = source.replace(/\r\n?/g, "\n");
               ^
TypeError: Object  has no method 'replace'
  at new Scanner (/Users/timaschew/project/node_modules/eco/lib/scanner.js:31:28)
  at new Preprocessor (/Users/timaschew/project/node_modules/eco/lib/preprocessor.js:17:22)
  at module.exports.Preprocessor.preprocess (/Users/timaschew/project/node_modules/eco/lib/preprocessor.js:12:22)
  at exports.precompile.precompile (/Users/timaschew/project/node_modules/eco/lib/compiler.js:12:35)
  at exports.compile (/Users/timaschew/project/node_modules/eco/lib/compiler.js:19:38)
  at module.exports.eco (/Users/timaschew/project/node_modules/eco/lib/index.js:11:86)
  at Function.eco.render (/Users/timaschew/project/node_modules/eco/lib/index.js:26:13)
  at Object.eval (<anonymous>:74:27)
  at Object.eval (<anonymous>:106:8)
  at eval (<anonymous>:108:6)
  at Function.eco.render (/Users/timaschew/project/node_modules/eco/lib/index.js:26:25)
  at /Users/timaschew/project/tools/create_makefile.coffee:90:17, <js>:107:43
  at fs.js:266:14
  at Object.oncomplete (fs.js:107:15)