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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/util/engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ var engines = module.exports;
// placeholder opening tag. This is often useful for templates including
// snippet of templates you don't want to be interpolated.

var matcher = /<%%([^%]+)%>/g;
var detecter = /<%%?[^%]+%>/;
engines.underscore = function underscore(source, data, options) {
source = source.replace(matcher, function (m, content) {
source = source.replace(engines.underscore.options.matcher, function (m, content) {
// let's add some funny markers to replace back when templating is done,
// should be fancy enough to reduce frictions with files using markers like
// this already.
Expand All @@ -33,12 +31,19 @@ engines.underscore = function underscore(source, data, options) {
source = _.template(source, null, options)(data);

source = source
.replace(/\(;>%%<;\)/g, '<%')
.replace(/\(;>%<;\)/g, '%>');
.replace(/\(;>%%<;\)/g, engines.underscore.options.start)
.replace(/\(;>%<;\)/g, engines.underscore.options.end);

return source;
};

engines.underscore.options = {
matcher: /<%%([^%]+)%>/g,
detecter: /<%%?[^%]+%>/,
start: '<%',
end: '%>'
};

engines.underscore.detect = function detect(body) {
return detecter.test(body);
return engines.underscore.options.detecter.test(body);
};
31 changes: 31 additions & 0 deletions test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,37 @@ describe('yeoman.generators.Base', function () {
assert.textEqual(body, '<version>bar</version> <%= foo %>;\n');
});
});

describe('with custom tags', function () {
beforeEach(function (done) {
this.src = 'custom-template-setting.xml';
this.dest = 'write/to/custom-template-setting.xml';

var oldEngineOptions = this.dummy.options.engine.options;

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

this.dummy.template(this.src, this.dest, { foo: 'bar' }, {
evaluate: /\{\{([\s\S]+?)\}\}/g,
interpolate: /\{\{=([\s\S]+?)\}\}/g,
escape: /\{\{-([\s\S]+?)\}\}/g
});

this.dummy.conflicter.resolve(done);

this.dummy.options.engine.options = oldEngineOptions;
});

it('uses tags specified in option and engine', function () {
var body = fs.readFileSync(this.dest, 'utf8');
assert.textEqual(body, '<version>bar</version> {{ foo }}\n');
});
});
});

describe('generator.directory(source, destination, process)', function () {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/custom-template-setting.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<version>{{= foo }}</version> {{{ foo }}