jonschlinkert / engine

Alternative to Lo-Dash template with support for helpers.
https://github.com/jonschlinkert
Other
17 stars 5 forks source link

How to escape the expressions ${myVar} in a engine template ? #5

Open rbecheras opened 6 years ago

rbecheras commented 6 years ago

I use Generate to create my own generators and engine as template engine.

When in a template I have a line including a template string (${myVar}), engine seems trying to find the myVar value to inject it in the template.

But here I'm generating a file containing some template strings like in es6 template strings of bash template string.

See below:

As you can see, here the template for the generating file .gitlab-ci.yml include the following line:

name: ${CI_COMMIT_REF_NAME}${CI_JOB_NAME}

But I need this line to be unchanged after the template have been rendered.

  1. How to escape the expressions ${myVar} in a engine template ?
  2. Is the expressions ${myVar} and <%= myVar %> equivalent ?
jonschlinkert commented 6 years ago

I believe you should be able to escape the template by doing \${foo}. I'm not sure if that will work, let us know either way and we'll go from there.

(fwiw, I get all of the notifications from GitHub, feel free to tweet as well, but it's just a duplicate notif to me. I saw a bunch of the notifs while I was on vacation, I'm back now. Sorry for the delay)

edit: btw this template-escaping issue has been one of the most frustrating aspects of doing these templating libraries. there are so many scenarios like this where different syntaxes collide in some way, or where users need to do documentation for templates but they don't want the templates inside markdown fences to be rendered, etc. There are several ways we can work around this, I just need to know more about your build and if that suggestion I gave worked.

rbecheras commented 6 years ago

Hi Jon!

Sorry for the duplicated notifications, it's noticed, github notifications are fine.

Sorry too I forgot to mention I already tried \${foo} and it don't work, it behave exactly like ${foo} .

rbecheras commented 6 years ago

Hi Jon,

For now I use a workaround but let me know what you think about it:

jonschlinkert commented 6 years ago

We can update the regex to allow templates to be escaped. However, if you need to render those escaped templates at some other point in the process, you will need to unescape them first. I'll take a look at the regex and see what needs to be done.