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

Allow full expressions in statements #3

Open
lazd opened this issue Feb 22, 2014 · 2 comments
Open

Allow full expressions in statements #3

lazd opened this issue Feb 22, 2014 · 2 comments

Comments

@lazd
Copy link
Owner

lazd commented Feb 22, 2014

Expressions should be allowed in statements:

  1. {{variable+1}}
  2. {{variable+method()}}
  3. {{method(variable+otherVariable)}}
  4. <if variable||otherVariable>
@cespare
Copy link

cespare commented Mar 11, 2015

Yeah, <if var == "foo"> was something I just wanted today.

@lazd
Copy link
Owner Author

lazd commented Mar 11, 2015

@cespare you can accomplish the equality comparison with a quick helper:

Helper:

function eq(a, b) {
  return a === b;
}

Template:

<if eq(data.foo,"bar")>
  <div>Yay!</div>
</if>

Output:

(function anonymous(data_0) {
  var frag = document.createDocumentFragment();
  var data = data_0;
  if (eq(data_0["foo"],"bar")) {
    var el2 = document.createElement("div");
    el2.textContent = "Yay!";
    frag.appendChild(el2);
  }
  return frag;
})

This is what we've had to do in Handlebars for years, so I'd definitely like to see this possible in DOMly. However, the whole statement needs to be a valid attribute name according to Cheerrio's HTML parser, so its impossible to throw an equals sign in there. This won't parse the way you expect it:

<div if-data.bar==='baz'='class="bazified"'>Yay!</div>

However, using the helper approach, you can easily test equality in a conditional attribute:

<if eq(data.foo,'bar')>
  <div if-eq(data.bar,'baz')='class="bazified"'>Yay!</div>
</if>

Output:

(function anonymous(data_0) {
  var frag = document.createDocumentFragment();
  var data = data_0;
  if (eq(data_0["foo"],'bar')) {
    var el2 = document.createElement("div");
    if (eq(data_0["bar"],'baz')) {
      el2.className = "bazified";
    }
    el2.textContent = "Yay!";
    frag.appendChild(el2);
  }
  return frag;
})

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