engine icon indicating copy to clipboard operation
engine copied to clipboard

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

Open rbecheras opened this issue 8 years ago • 4 comments

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:

  • generate generator project: https://github.com/rbecheras/generate-swap-project
  • issue: https://github.com/rbecheras/generate-swap-project/issues/31
  • PR: https://github.com/rbecheras/generate-swap-project/pull/80
  • travis failling: https://travis-ci.org/rbecheras/generate-swap-project/builds/326427995?utm_source=github_status&utm_medium=notification
  • My template: https://github.com/rbecheras/generate-swap-project/blob/31-generate-gitlab-ci/src/assets/templates/generate-swap-gitlabci/_gitlab-ci.yml
  • The line in the template that throws th ReferenceError: https://github.com/rbecheras/generate-swap-project/blob/31-generate-gitlab-ci/src/assets/templates/generate-swap-gitlabci/_gitlab-ci.yml#L36
  • screenshots:
    • capture d ecran de 2018-01-08 17-34-12
    • capture d ecran de 2018-01-08 17-43-33

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 ?

rbecheras avatar Jan 08 '18 16:01 rbecheras

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.

jonschlinkert avatar Jan 08 '18 17:01 jonschlinkert

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 avatar Jan 09 '18 22:01 rbecheras

Hi Jon,

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

  • PR: https://github.com/rbecheras/generate-swap-project/pull/80
  • Workaround commits:
    • https://github.com/rbecheras/generate-swap-project/pull/80/commits/c8ab358e0b36897d2c84b3f5bfe6a1a234249b2c
    • https://github.com/rbecheras/generate-swap-project/pull/80/commits/6b552dcfba020006815fb46c95730db4c933827d
  • Passing tests: https://travis-ci.org/rbecheras/generate-swap-project/builds/327214195?utm_source=github_status&utm_medium=notification

rbecheras avatar Jan 10 '18 12:01 rbecheras

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.

jonschlinkert avatar Jan 11 '18 13:01 jonschlinkert