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

Cannot expand variable in comment #1311

Closed
gromgull opened this issue Dec 5, 2013 · 12 comments
Closed

Cannot expand variable in comment #1311

gromgull opened this issue Dec 5, 2013 · 12 comments

Comments

@gromgull
Copy link

gromgull commented Dec 5, 2013

Hi,

I'd like to have a variable expanded inside a comment for inserting a cloudfront server in a useminPrepare block:

// build:js #{cloudfront}js/app.min.js

Just outputs the #{} verbatim:

    <!-- build:js(adminapp.src) #{cloudfront}js/testapp.min.js-->
  //
      = " build:js "+cloudfront+"js/app.min.js"

nearly works, but the closing comments ends up on a new line, which confuses useminPrepare:

<!-- build:js(adminapp.src) http://t/js/testapp.min.js
    -->

Is there any way around this?

@jdhealy
Copy link

jdhealy commented Dec 23, 2013

I think

| <!-- build:js(adminapp.src) #{cloudfront}js/testapp.min.js-->

should work.

@ForbesLindesay
Copy link
Member

Indeed, jade comments are deliberately pure comments, to allow people to type whatever they like in them.

@monolithed
Copy link

The comments are not displayed in the browser, but it the still full-fledged DOM-nodes!

//= BUILD: version

Much better than:

!='<!-- BUILD: ' + version + '-->'

Why not?

@ForbesLindesay
Copy link
Member

Because generated comments are currently second class citizens. Comments are designed for commenting rather than anything else.

@monolithed
Copy link

Template engines are designed to generate HTML code.
Even the JavaScript API allows us to fully manipulate comments:

function getCommentNodes(node) {
    var treeWalker = document.createTreeWalker(node,
        NodeFilter.SHOW_COMMENT, null, false),

        comments = [];

  while (treeWalker.nextNode()) {
        comments.push(treeWalker.currentNode);
  }

  return comments;
}

// Create a comment
var comment = document.createComment('foo');
document.body.appendChild(comment);

// Get all comments
var comments = getCommentNodes(document.body);

// Iterate over comments
comments.forEach(function (comment) {
  console.log(comment.data); // foo
});

I know a lot of template engines that support my point of view:

HAML:

/
  = variable

/
  #{variable}

EJS

 <!-- <%= variable %> -->

Dust

<--! {variable} -->

Mustache, Handlebars, Hogan

<!-- {{variable}} -->

Fest

<fest:comment>
    <fest:value>variable </fest:value>
</fest:comment>

I can not even remember any template engines that does not support interpolation comments. Only Jade?

@dbeff
Copy link

dbeff commented Feb 4, 2015

+1 @monolithed

@dopatraman
Copy link

why was this closed without resolution

@bickle66
Copy link

bickle66 commented Dec 2, 2015

I have to agree. I use gulp-useref. For cache busting I would love to be able to:

// build:js /js/app.min.js?v=#{version}
script(src="/js/app.js?v=" + version)
// endbuild

@ForbesLindesay
Copy link
Member

Because you can just do:

<!--build:js /js/app.min.js?v=#{version}-->
<!--endbuild-->

If you really need that and it's perfectly valid jade. It's also much better to do it this way if you're using this for builds since a lot of build tools that use comments tend to be really sensitive about precise spacing etc. and this gives you total control.

@Ulrikop
Copy link

Ulrikop commented Dec 5, 2016

I searched for the variable expanding at comments for the same reason and I found that issue.

I tried the solution with writting the comment directly in html:

<!--build:css #{folder}/app.css-->
<!--endbuild-->

The problem is, that I have something above the block:

meta(charset="utf-8")

<!--build:css #{folder}/app.css-->
<!--endbuild-->

After compiling pug the result is:

<meta charset="utf-8"><!--build:css #{folder}/app.css-->
<!--endbuild-->

The problem is that usemin removes all before build:css and I lose the meta tag (I use already the conig pretty = true)

I can add = "\n" but that is ugly. What is the prefered way?

@jhyland87
Copy link

Im also looking for a solution for this as well.

Obviously this isnt a very important issue, just more of an annoyance when you're going for some well-formatted source code, then this one thing throws it off.

@stemsmit
Copy link

stemsmit commented Jan 22, 2017

Ended up with this.

mixin bundleScripts(bundlePath)
    | #{'\n\t'}
    <!-- build:js(#{global.scriptsDest}) #{bundlePath} -->
    block
    | #{'\n\t'}
    <!-- endbuild -->
    | #{'\n\t'}

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

No branches or pull requests

10 participants