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

Added funtionality to change template delimiters in the template engine. #489

Closed
wants to merge 2 commits into from
Closed

Conversation

dtackett
Copy link
Contributor

@dtackett dtackett commented Feb 5, 2014

yeoman/generator-generator#45 and yeoman/yeoman#1235 talk about ways to pass options to lodash to change the template delimiters. However the template engine in yeoman does some detecting and has the %% escaping built in. If I try to template a file that doesn't have any <%``%> tags in the file it won't get templated.

This pull request adds the ability to provide options to the engine to customize the detection and escaping. For example the following configuration could be used to escape with {{``}} delimiters and escape a {{{ to {{.

engine.options = {
          detecter: /\{\{?[^\}]+\}\}/,
          matcher: /\{\{\{([^\}]+)\}\}/g,
          start: '{{',
          end: '}}'
        }

I also added tests that should cover this added configuration and would illustrate the issue without these changes.

@SBoudrias
Copy link
Member

Can you fix the build errors?

@coveralls
Copy link

Coverage Status

Coverage remained the same when pulling 4f6e509 on dtackett:master into 116b321 on yeoman:master.

@dtackett
Copy link
Contributor Author

dtackett commented Feb 5, 2014

Ah! Yes. Sorry about that. It should be fixed now.

@sambaker
Copy link

sambaker commented May 7, 2015

For anyone coming across this in 2015, the use of engine.options no longer works. To change the delimiters now, you pass a templateSettings object as the 4th argument to copyTpl as in:

this.fs.copyTpl(src, dest, context, {
  escape: /<$-([\s\S]+?)$>/g,
  evaluate: /<$([\s\S]+?)$>/g,
  interpolate: /<$=([\s\S]+?)$>/g
})

Which allows <$= ... $> instead of <%= ... %> etc

@lukeheath
Copy link

@sambaker - Thank you! That was driving me batty all weekend.

@purtuga
Copy link

purtuga commented Jul 12, 2015

@sambaker - Thank you as well... after much searching, your post above did it for me... but i did have to alter the regex in order for it to work... I had to escape the $... like this:

this.fs.copyTpl(this.templatePath('yourtemplate'), this.destinationPath('yourDestFile'), this, {
    escape:         /<\$-([\s\S]+?)\$>/g,
    evaluate:       /<\$([\s\S]+?)\$>/g,
    interpolate:    /<\$=([\s\S]+?)\$>/g
});

For any others: Yeoman documentation points to ejs.co for information on the templating engine... but none of these options are documented there... It does, however, mention the delimiter option, but I found it to not work.

(FYI: my use case: I was adding some file paths to a copy task in an already setup Gruntfile.js, so I needed the template tags to be different from those already used in the Gruntfile.

@SBoudrias
Copy link
Member

@purtuga this mean you're not using the latest version ;) Our documentation follow our latest release, it looks like you're still on 0.19

@purtuga
Copy link

purtuga commented Jul 13, 2015

I thought I was running the latest, since I installed yeoman/generator-generator for the first time last week. But I'll check and report back. Thanks for letting me know. 

Paul
-- sent from mobile

@purtuga
Copy link

purtuga commented Jul 19, 2015

@SBoudrias - Just checked...yes.. still on 0.19.2. Will upgrade. Thanks.

@purtuga
Copy link

purtuga commented Jul 25, 2015

Just wanted to report back... After upgrading to v0.20.2 of generator-generator, the delimiter option mentioned in ejs's documentation works...

this.fs.copyTpl(this.templatePath('yourtemplate'), this.destinationPath('yourDestFile'), this, {
    delimiter: "$"
});

@gerrard00
Copy link

Thanks @purtuga, I was confused trying to get the deprecated syntax to work.

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

Successfully merging this pull request may close these issues.

7 participants