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

logical operators templates.parse support #1065

Closed
akhoury opened this issue Feb 20, 2014 · 13 comments
Closed

logical operators templates.parse support #1065

akhoury opened this issue Feb 20, 2014 · 13 comments
Milestone

Comments

@akhoury
Copy link
Member

akhoury commented Feb 20, 2014

if would be nice if you can just evaluate the expression in JS if truthy or not
<!-- IF (foo && bar) || (you && me) || okletmein -->

@psychobunny
Copy link
Contributor

This isn't something that is supported in handlebars but I've waiting for
someone to request this so I can build it in, I already figured out how to
accomplish it ;)
On 2014-02-19 11:50 PM, "Aziz Khoury" notifications@github.com wrote:

if would be nice if you can just evaluate the expression in JS it truthy
or not

Reply to this email directly or view it on GitHubhttps://github.com//issues/1065
.

@akhoury
Copy link
Member Author

akhoury commented Feb 20, 2014

Awesome thanks!
you're right, it's not supported in handlebars, but if can be done using a helper.

@psychobunny
Copy link
Contributor

Yeah lets see :) it can be done with some other engines (ironically
supported in Smarty). More complex conditionals like array.length > x is
something I want to try too. I refuse to eval() if I can help it but I have
an idea. Have just been waiting until it's actually needed so since you
requested it I can give it a shot
On 2014-02-20 12:59 AM, "Aziz Khoury" notifications@github.com wrote:

Awesome thanks!
you're right, it's not supported in handlebars, but if can be done using a
helper.

Reply to this email directly or view it on GitHubhttps://github.com//issues/1065#issuecomment-35591702
.

@psychobunny
Copy link
Contributor

Although maybe integrating a helper system might be easier overall... hmm...

(Or switch to handlebars /trollshield activate)
On 2014-02-20 1:11 AM, "Andrew Rodrigues" rodrigues.andrew@gmail.com
wrote:

Yeah lets see :) it can be done with some other engines (ironically
supported in Smarty). More complex conditionals like array.length > x is
something I want to try too. I refuse to eval() if I can help it but I have
an idea. Have just been waiting until it's actually needed so since you
requested it I can give it a shot
On 2014-02-20 12:59 AM, "Aziz Khoury" notifications@github.com wrote:

Awesome thanks!
you're right, it's not supported in handlebars, but if can be done using
a helper.

Reply to this email directly or view it on GitHubhttps://github.com//issues/1065#issuecomment-35591702
.

@psychobunny
Copy link
Contributor

For now its okay to nest your IF statements, I'm assigning as low priority because we haven't seen a real dire need for this in core just yet. ETA post 0.4x

@akhoury
Copy link
Member Author

akhoury commented Feb 20, 2014

haha, why do you refuse to use eval?
ok how about something without literally using eval. I did something similar here

var IFEVAL = function (expression, parseData) {
   var fn;
   try {
     // create a new function from code
     // pass the parseData so that could be the context of the function
     // app, meta, foo, bar these are just example arguments 
     // that you can pass to every functions and use inside expressions if needed, so your expression could be something like:
    //         topic.title && typeof meta.config.something == \'number\' && app.yo
   // yes you would need to escape quotes, well, it depends.. if single or double, but you get the gist
     fn = Function.apply(parseData, ['app, meta, foo, bar', 'return ' + expression + ' ; ' ] );
   } catch (e) {
     fn = function(){return false;}; 
   }
   if ( fn(app, meta, foo, bar) ) {
      /// .... print IF block, or revaluate
   } else {
      // .... print ELSE block if there is one
   }
}

i dont know something like that, you could also just have an EVAL helper that will just output the result of the expression, no?

@psychobunny
Copy link
Contributor

Interesting 👻 I'll have a think about that, good idea.

haha, why do you refuse to use eval?

trying to reduce the amount of tpd* I get

*trolls per day

@akhoury
Copy link
Member Author

akhoury commented Feb 20, 2014

here's a Handlebars gist, if you ever end up switching to it :P

https://gist.github.com/akhoury/9118682

@psychobunny psychobunny added this to the 0.4.0 milestone Mar 17, 2014
@julianlam
Copy link
Member

@psychobunny Did you still want this in for 0.4.0?

@psychobunny
Copy link
Contributor

Yup. Helpers are in
On 2014-03-19 10:31 AM, "Julian Lam" notifications@github.com wrote:

@psychobunny https://github.com/psychobunny Did you still want this in
for 0.4.0?

Reply to this email directly or view it on GitHubhttps://github.com//issues/1065#issuecomment-38056722
.

@julianlam
Copy link
Member

Oh, we're going to use helpers instead of logical operators? I think that's
better anyway 👍

@psychobunny
Copy link
Contributor

k just merged it in, ex:

templates.registerHelper('somefn', function(data) {
    return (data.propOne && data.propTwo) ? "Something" : "another";
});

or

templates.registerHelper('somefn', function(array, iterator, total) {
    return array[iterator].someProperty || array[iterator].another ? "something" : "something else";
});

@cnvo
Copy link
Contributor

cnvo commented Mar 28, 2014

Nice work @psychobunny 👍

On Fri, Mar 28, 2014 at 4:23 PM, psychobunny notifications@github.comwrote:

Closed #1065 #1065.


Reply to this email directly or view it on GitHubhttps://github.com//issues/1065
.

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

4 participants