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

Tag attributes value must be "string forced" #3

Closed
thibaudgg opened this issue Nov 16, 2011 · 7 comments
Closed

Tag attributes value must be "string forced" #3

thibaudgg opened this issue Nov 16, 2011 · 7 comments

Comments

@thibaudgg
Copy link

This should be possible:

%div{ class: @item.name }

but to make it work, I must done:

%div{ class: "#{@item.name}" }

Otherwise, it works super great. Thanks a lot!

@netzpirat
Copy link
Collaborator

This is possible! Even more complex functions can be called.

$ echo "%div{ class: @item.name }" > test.hamlc
$ ./bin/haml-coffee -i test.hamlc 
[Haml Coffee] Compiling file test.hamlc to test.jst
$ cat test.jst
(function() {
  var _base, _ref;

  if ((_ref = window.HAML) == null) window.HAML = {};

  (_base = window.HAML).htmlEscape || (_base.htmlEscape = function(text) {
    return ("" + text).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&apos;').replace(/"/g, '&quot;');
  });

  window.HAML['test'] = function(context) {
    var fn;
    fn = function(context) {
      var e, o;
      o = [];
      e = window.HAML.htmlEscape;
      o.push("<div class='" + (e(this.item.name)) + "'></div>");
      return o.join("\n");
    };
    return fn.call(context);
  };

}).call(this);

Is there more context in that template?

As you have found out by yourself, enforcing interpolation by quoting the expression enables much robust parsing and is always a handy fallback when something does not work.

@netzpirat
Copy link
Collaborator

The previous test was with haml-coffe. To also verify haml_coffee_assets, I took the example from #2 and modified it a bit:

#cart
  %h2= I18n.t('js.cart.title')

  - if @cart.length == 0
    %p.empty= I18n.t('js.cart.empty')

  - else
    %ul
      - for item in @cart
        %li
          .item
            = item.name
            %a{ href: item.id }
              = I18n.t('js.cart.item.remove')

Also in this case I got a template:

function (context) {
    var fn;
    fn = function(context) {
      var e, item, o, _i, _len, _ref2;
      o = [];
      e = HAML.escape;
      o.push("<div id='cart'>");
      o.push("  <h2>" + (e(I18n.t('js.cart.title'))) + "</h2>");
      if (this.cart.length === 0) {
        o.push("  <p class='empty'>" + (e(I18n.t('js.cart.empty'))) + "</p>");
      } else {
        o.push("  <ul>");
        _ref2 = this.cart;
        for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
          item = _ref2[_i];
          o.push("    <li>");
          o.push("      <div class='item'>");
          o.push(e("        " + item.name));
          o.push("        <a href='" + (e(item.id)) + "'>");
          o.push(e("          " + I18n.t('js.cart.item.remove')));
          o.push("        </a>");
          o.push("      </div>");
          o.push("    </li>");
        }
        o.push("  </ul>");
      }
      o.push("</div>");
      return o.join("\n");
    };
    return fn.call(App.application.globalTemplateContext(context));
  }

And item.id works fine as attribute:

o.push("        <a href='" + (e(item.id)) + "'>");

@thibaudgg
Copy link
Author

Ok, I'll give it a try again.

@netzpirat
Copy link
Collaborator

Make sure to use the haml_coffee_assets gem and not haml-coffee-rails or ruby-haml-coffe, because they don't use my enhanced haml-coffee fork.

@thibaudgg
Copy link
Author

Ok so with haml_coffee_assets when I got something like:

- square = (i) -> "#{i * i}"
- for i in [[1, 2],[3, 4]]
  %span{ class: square(i[0]) }= square(i[0])

it brokes, with something like:

- square = (i) -> "#{i * i}"
- for i in [1..4]
  %span{ class: square(i) }= square(i)

it's fine. Normal? :)

@netzpirat
Copy link
Collaborator

This is a bug and has been fixed with the 0.2.3 release.

The lexer handles attributes in different ways:

  • Quoted attributes are more simple, because I can detect when a quote ends (even when it has escaped quotes in between), and everything in between is a simple string where you can use interpolations.
  • Attributes like test: @model.get('name') are more complex, because I can not surely say where it ends, so I have to positively match all the allowed characters.

I really like such feedback, it helps tweak the lexer regular expressions for things I have forgotten.

@thibaudgg
Copy link
Author

Works like a charm now. Thanks Micheal!

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

No branches or pull requests

2 participants