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

Compile + invoke templates immediately? #35

Open
cowboy opened this issue Nov 4, 2013 · 8 comments
Open

Compile + invoke templates immediately? #35

cowboy opened this issue Nov 4, 2013 · 8 comments

Comments

@cowboy
Copy link
Member

cowboy commented Nov 4, 2013

There are 2 main use-cases I see for "javascript" templates:

  1. Compile template files into JavaScript functions for use on the client. These templates will be namespaced and combined into a single .js file.
  2. Compile template files into JavaScript functions for use on the server. These templates will be run immediately, with the output being saved into .html files.

This plugin already does 1, but what about 2? In my example, I'm already using lo-dash templates in the client via requirejs plugin, but I don't want to use Jade for the app's index.html page. I also want to use lo-dash templates.

So, in my project Gruntfile, I created this "tmpl" task, but it seems a little redundant for this to be a separate thing.

grunt.registerMultiTask('tmpl', 'compile lodash templates to html files', function() {
  var _ = require('lodash');
  var options = this.options({
    data: {},
    templateSettings: null,
  });
  var origTemplateSettings = _.templateSettings;
  this.files.forEach(function(f) {
    var src = grunt.file.read(f.src[0]);
    if (options.templateSettings) {
      _.templateSettings = _.extend(_.templateSettings, options.templateSettings);
    }
    var html = _.template(src, options.data);
    grunt.file.write(f.dest, html);
    _.templateSettings = origTemplateSettings;
  });
});

Is there a place in grunt-contrib-jst for the "2" behavior?

@tkellen
Copy link
Member

tkellen commented Nov 4, 2013

Yes, there is definitely a place for it. We were close to settling on a standard across all templating tasks before I disappeared into the woods. The relevant conversation is here: gruntjs/grunt-contrib-handlebars#26

@cowboy
Copy link
Member Author

cowboy commented Nov 4, 2013

Ok, well it seems like this should happen for grunt-contrib-handlebars and grunt-contrib-jade, but in a consistent way. So let's discuss the solution for all 3 plugins here.

Some questions:

  1. Is it as simple as compiling one input template file + some data + optional settings into one output file?
  2. Do we need to worry about partials? If so, how do we handle this, by specifying a single input file + a baseDir where the partials will live?
  3. What about an option render that, when set to true, renders the template with the specified data object?

/cc @tbranyen @shama @sindresorhus @vladikoff

@cowboy
Copy link
Member Author

cowboy commented Nov 4, 2013

I'm guessing that if a plugin needed partials, we'd already be specifying some kind of baseDir option. Right?

@tkellen
Copy link
Member

tkellen commented Nov 4, 2013

Unless I am misunderstanding you, a baseDir would be meaningless for jst/handlebars, as they don't have the concept of file io built in like jade does.

@vladikoff
Copy link
Member

/cc @oswaldoacauan @mitsuruog @pspeter3 @tnguyen14 @outaTiME @mehcode pinging a few devs who were interested in this feature and hopefully have some input.

@cowboy
Copy link
Member Author

cowboy commented Nov 5, 2013

@tkellen correct, but in case we ever added another templating plugin that supported partials, it seemed like a good "standard" option to define.

@cowboy
Copy link
Member Author

cowboy commented Nov 5, 2013

For example, I see a few options in grunt-contrib-handlebars for dealing with partials, but they seem to be a bit ad-hoc, and confusing to me. Could configuring partials support for grunt-contrib-handlebars and grunt-contrib-jade be done in more general ways?

@mehcode
Copy link

mehcode commented Nov 5, 2013

Something to perhaps reference. We have implemented static compilation / partials in https://github.com/concordusapps/grunt-haml. From a pull request we also have static compilation to JS variables (basically take the HTML and assign them to JS variables in a big file) -- seems a bit weird to me but I don't use server-side templates too much.

If anything, I'll make sure to watch this space and update the plugin to conform to whatever is decided.

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

No branches or pull requests

4 participants