Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ES6 Template like strings in ES3 compatible syntax.
// this is now a module:
// https://github.com/WebReflection/backtick-template#es2015-backticks-for-es3-engines--
var template = require('backtick-template');
// just string
const info = 'template';
`some ${info}` === template('some ${info}', {info});
// passing through a transformer
transform `some ${info}` ===
template(transform, 'some ${info}', {info});
// using it as String method
String.prototype.template = template.asMethod;
`some ${info}` === 'some ${info}'.template({info});
transform `some ${info}` ===
'some ${info}'.template(transform, {info});
@alundiak

This comment has been minimized.

Copy link

alundiak commented Sep 4, 2015

Is this kinda proposal to get rid of ES6 way? Any luck to discuss it with TC39?

@aMarCruz

This comment has been minimized.

Copy link

aMarCruz commented Sep 10, 2015

👍
In line# 16 you can write str = this.slice(i, m.index);
instead str = this.slice(i, re.lastIndex - m[0].length);

@WebReflection

This comment has been minimized.

Copy link
Owner Author

WebReflection commented Sep 14, 2015

fixed, will update the CSP version too soon, cheers

@hooblei

This comment has been minimized.

Copy link

hooblei commented Oct 28, 2015

var f = function (_) { return 'foo'; }, v = 1;
`hm ... ${f({a: v})}`
"hm ... foo"
"hm ... ${f({a: v})}".template({
  f: function () { return 'foo'; },
  v: 1
});
undefined:2
with(this)return "hm ... "+(f({a: v)+")}"
                                   ^
SyntaxError: Unexpected token )
    at Function (<anonymous>)
    ...

... and fixed

@JaroslavMoravec

This comment has been minimized.

Copy link

JaroslavMoravec commented Jun 13, 2016

+1 for @hooblei - there is bad RE which makes } inside embedded expressions impossible.

@ramtob

This comment has been minimized.

Copy link

ramtob commented Sep 7, 2016

+1

@bcomnes

This comment has been minimized.

Copy link

bcomnes commented Dec 11, 2016

Has anyone turned this into a module instead of a prototype extend?

@WebReflection

This comment has been minimized.

Copy link
Owner Author

WebReflection commented Jan 16, 2017

@aMarCruz @hooblei @JaroslavMoravec @ramtob @bcomnes

I've rewritten the original idea from scratch and covered 100% (let's say it was an excuse to test coveralls but ... hey ...)

https://github.com/WebReflection/backtick-template#es2015-backticks-for-es3-engines--

Best Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.