{{elsif}} or {{elseif}} or {{#elseif}} #39

Closed
ramezrafla opened this Issue May 13, 2016 · 33 comments

Comments

Projects
None yet
9 participants
@ramezrafla

I am suggesting we add an {{elsif}} or {{elseif}} or whatever variation to avoid this ugly piece of code:

{{#if condition1}}
{{else}}
  {{#if condition2}}
  {{else}}
     {{#if condition3}}
     {{/if}}
  {{/if}}
{{/if}}

Imagine this instead

{{#if condition1}}
{{elseif condition2}}
{{elseif condition3}}
{{/if}}

I also think this will go a long way in making people more likely to adopt Blaze as it cleans up the code quite a bit for complex apps.

@ramezrafla ramezrafla changed the title from {{elsif}} to {{elsif}} or {{elseif}} or {{#elseif}} May 13, 2016

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar May 13, 2016

Collaborator

Does handlebars have it?

Collaborator

mitar commented May 13, 2016

Does handlebars have it?

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar May 13, 2016

Collaborator

The issue is that this might seem a simple addition but would require a lot of changes internally. Like, a lot.

Collaborator

mitar commented May 13, 2016

The issue is that this might seem a simple addition but would require a lot of changes internally. Like, a lot.

@ramezrafla

This comment has been minimized.

Show comment
Hide comment
@ramezrafla

ramezrafla May 13, 2016

Handlebars does not seem to have it.
I was thinking .. a preprocessor would do the trick :)

[Edit] To clarify, a preprocessor could simply use regexp (or some sort of parser) to replace the new syntax with the old one internally before it is processed.
Also, this preprocessor can allow future improvements without touching the code. Given it only runs once per template, the overhead is negligible.

ramezrafla commented May 13, 2016

Handlebars does not seem to have it.
I was thinking .. a preprocessor would do the trick :)

[Edit] To clarify, a preprocessor could simply use regexp (or some sort of parser) to replace the new syntax with the old one internally before it is processed.
Also, this preprocessor can allow future improvements without touching the code. Given it only runs once per template, the overhead is negligible.

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar May 13, 2016

Collaborator

Hmmm. Maybe. So at the level or parser. Like a syntactic sugar. This could work. You could look into spacebars parser if you feel adventurous. :-)

Collaborator

mitar commented May 13, 2016

Hmmm. Maybe. So at the level or parser. Like a syntactic sugar. This could work. You could look into spacebars parser if you feel adventurous. :-)

@ramezrafla

This comment has been minimized.

Show comment
Hide comment
@ramezrafla

ramezrafla May 13, 2016

Got it :) -- will start digging into it. Is the parser part of this repo or somewhere else?

Got it :) -- will start digging into it. Is the parser part of this repo or somewhere else?

@AdrienTorris

This comment has been minimized.

Show comment
Hide comment
@AdrienTorris

AdrienTorris Jul 22, 2016

totally agreed with this issue. Simple if/else if/else is just so usefull

totally agreed with this issue. Simple if/else if/else is just so usefull

nathantreid added a commit to nathantreid/blaze that referenced this issue Jul 28, 2016

@nathantreid

This comment has been minimized.

Show comment
Hide comment
@nathantreid

nathantreid Jul 28, 2016

I've been wanting this every time I write an ugly if else if else chain, and today it caught my interest, so I've implemented a version using {{elseif}} (example taken from original post):

{{#if condition1}}
{{elseif condition2}}
{{elseif condition3}}
{{/if}}

As suggested, it is simply sugar syntax. The above example is transformed into the compiled spacebars version of this:

{{#if condition1}}
{{else}}
  {{#if condition2}}
  {{else}}
     {{#if condition3}}
     {{/if}}
  {{/if}}
{{/if}}

nathantreid commented Jul 28, 2016

I've been wanting this every time I write an ugly if else if else chain, and today it caught my interest, so I've implemented a version using {{elseif}} (example taken from original post):

{{#if condition1}}
{{elseif condition2}}
{{elseif condition3}}
{{/if}}

As suggested, it is simply sugar syntax. The above example is transformed into the compiled spacebars version of this:

{{#if condition1}}
{{else}}
  {{#if condition2}}
  {{else}}
     {{#if condition3}}
     {{/if}}
  {{/if}}
{{/if}}
@ramezrafla

This comment has been minimized.

Show comment
Hide comment
@ramezrafla

ramezrafla Jul 28, 2016

Excellent @nathantreid,
Can you PR with your changes?

Excellent @nathantreid,
Can you PR with your changes?

@nathantreid

This comment has been minimized.

Show comment
Hide comment
@nathantreid

nathantreid Jul 28, 2016

@ramezrafla Check out the PR link above my previous post. 😄

nathantreid commented Jul 28, 2016

@ramezrafla Check out the PR link above my previous post. 😄

@ramezrafla

This comment has been minimized.

Show comment
Hide comment
@ramezrafla

ramezrafla Jul 28, 2016

Oops - sorry - I'll blame it on small iPhone screen :) thanks a lot!

Oops - sorry - I'll blame it on small iPhone screen :) thanks a lot!

@meteorplus

This comment has been minimized.

Show comment
Hide comment
@meteorplus

meteorplus Aug 19, 2016

When is this coming out? coming soon this summer 2017?

When is this coming out? coming soon this summer 2017?

@nathantreid

This comment has been minimized.

Show comment
Hide comment
@nathantreid

nathantreid Aug 19, 2016

@capensisma: Quoting Mitar (from my pull request):

Waiting for #20 and that we release first community Blaze version. I would like that one to be without extra features, just bugfixes. And then we can start adding features.

@capensisma: Quoting Mitar (from my pull request):

Waiting for #20 and that we release first community Blaze version. I would like that one to be without extra features, just bugfixes. And then we can start adding features.

@ramezrafla

This comment has been minimized.

Show comment
Hide comment
@ramezrafla

ramezrafla Dec 16, 2016

Is there anything gating this? @mitar, want me to run some tests first?

Is there anything gating this? @mitar, want me to run some tests first?

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Dec 16, 2016

Collaborator

See the pull request #50. I would like that this is implemented compatible with Handlebars which allows any block helper to be used. So you can do:

{{#tab title="Foo"}}

{{else tab title="Bar"}}

{{else tab title="Baz"}}

{{/tab}}

Feel free to make a pull request like that. With tests and documentation. That would be great!

Collaborator

mitar commented Dec 16, 2016

See the pull request #50. I would like that this is implemented compatible with Handlebars which allows any block helper to be used. So you can do:

{{#tab title="Foo"}}

{{else tab title="Bar"}}

{{else tab title="Baz"}}

{{/tab}}

Feel free to make a pull request like that. With tests and documentation. That would be great!

@ramezrafla

This comment has been minimized.

Show comment
Hide comment
@ramezrafla

ramezrafla Dec 16, 2016

Thanks @mitar, we can certainly adopt this syntax, but we can also add the other syntax above too. The two can be exclusive. Blaze has evolved quite a bit from Handlebars syntax. I personally like having elseif's -- it's more natural and less convoluted and would add value to developers.

ramezrafla commented Dec 16, 2016

Thanks @mitar, we can certainly adopt this syntax, but we can also add the other syntax above too. The two can be exclusive. Blaze has evolved quite a bit from Handlebars syntax. I personally like having elseif's -- it's more natural and less convoluted and would add value to developers.

@mitar mitar closed this in 2fa3cab Jan 1, 2017

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Jan 1, 2017

Collaborator

With 2fa3cab I implemented the Handlebars syntax for chaining block tags. After further consideration I did not implement {{elseif}} as well because it opens a small backwards incompatibility (somebody could have a template helper named elseif). Let's try the {{else if}} syntax and see how it will work. I started to like it, especially because you can use it with custom block tags.

Collaborator

mitar commented Jan 1, 2017

With 2fa3cab I implemented the Handlebars syntax for chaining block tags. After further consideration I did not implement {{elseif}} as well because it opens a small backwards incompatibility (somebody could have a template helper named elseif). Let's try the {{else if}} syntax and see how it will work. I started to like it, especially because you can use it with custom block tags.

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Jan 12, 2017

Collaborator

Released as 2.3.0. You have to update templating package.

Collaborator

mitar commented Jan 12, 2017

Released as 2.3.0. You have to update templating package.

@namirsab

This comment has been minimized.

Show comment
Hide comment
@namirsab

namirsab May 17, 2017

@mitar and the rest, first of all, thanks a lot for keeping blaze alive.

I'm writing here even if it's closed just to point one thing.
It was difficult to me to know which packages i had to update. meteor test was not building the app (because of the if/else if thingy) even if it had the correct blaze version and it was because templating was not up to date.

Is it written anywhere which packages are needed to be updated?

Again, thanks a lot!

namirsab commented May 17, 2017

@mitar and the rest, first of all, thanks a lot for keeping blaze alive.

I'm writing here even if it's closed just to point one thing.
It was difficult to me to know which packages i had to update. meteor test was not building the app (because of the if/else if thingy) even if it had the correct blaze version and it was because templating was not up to date.

Is it written anywhere which packages are needed to be updated?

Again, thanks a lot!

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar May 17, 2017

Collaborator

meteor update should update all? So all packages in this repository.

Collaborator

mitar commented May 17, 2017

meteor update should update all? So all packages in this repository.

@namirsab

This comment has been minimized.

Show comment
Hide comment
@namirsab

namirsab May 17, 2017

but meteor update also updates the meteor tool and so on. What if i just want to update blaze?

but meteor update also updates the meteor tool and so on. What if i just want to update blaze?

@maxfi

This comment has been minimized.

Show comment
Hide comment
@maxfi

maxfi Jun 11, 2017

Released as 2.3.0. You have to update templating package.

Is this mentioned somewhere in the docs? I couldn't find anything. Thanks.

maxfi commented Jun 11, 2017

Released as 2.3.0. You have to update templating package.

Is this mentioned somewhere in the docs? I couldn't find anything. Thanks.

@mitar

This comment has been minimized.

Show comment
Hide comment
@maxfi

This comment has been minimized.

Show comment
Hide comment
@maxfi

maxfi Jun 18, 2017

Thanks @mitar. I expected there to be a mention here: http://blazejs.org/guide/spacebars.html#If-Unless

maxfi commented Jun 18, 2017

Thanks @mitar. I expected there to be a mention here: http://blazejs.org/guide/spacebars.html#If-Unless

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Jun 18, 2017

Collaborator

Feel free to make a pull request. :-)

Collaborator

mitar commented Jun 18, 2017

Feel free to make a pull request. :-)

@wei170

This comment has been minimized.

Show comment
Hide comment
@wei170

wei170 Jul 18, 2017

My blaze version is on 2.3.2. But I still cannot use
{{#if foo}} {{else if boo}} {{else}} {{/if}}

wei170 commented Jul 18, 2017

My blaze version is on 2.3.2. But I still cannot use
{{#if foo}} {{else if boo}} {{else}} {{/if}}

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Jul 19, 2017

Collaborator

Which version of spacebars-compiler package you have?

Collaborator

mitar commented Jul 19, 2017

Which version of spacebars-compiler package you have?

@wei170

This comment has been minimized.

Show comment
Hide comment
@wei170

wei170 Jul 19, 2017

My spacebars-compiler is @1.1.2

wei170 commented Jul 19, 2017

My spacebars-compiler is @1.1.2

@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Jul 19, 2017

Collaborator

Then please create a small reproduction (a repo showcasing ony this issue).

Collaborator

mitar commented Jul 19, 2017

Then please create a small reproduction (a repo showcasing ony this issue).

@wei170

This comment has been minimized.

Show comment
Hide comment
@wei170

wei170 Jul 19, 2017

{{#if isUserProfile}}
        <h3>User Profile</h3>
{{else if isLawyerProfile}}
        <h3>Lawyer Profile</h3>
{{else}}
        <h3>Test</h3>
{{/if}}

Error:

Errors prevented startup:
While processing files with templating-compiler (for target web.browser): client/profile/Profile.html:8: Expected `}}`
...ofile</h3>     {{else if isLawyerProfile}...

wei170 commented Jul 19, 2017

{{#if isUserProfile}}
        <h3>User Profile</h3>
{{else if isLawyerProfile}}
        <h3>Lawyer Profile</h3>
{{else}}
        <h3>Test</h3>
{{/if}}

Error:

Errors prevented startup:
While processing files with templating-compiler (for target web.browser): client/profile/Profile.html:8: Expected `}}`
...ofile</h3>     {{else if isLawyerProfile}...
@mitar

This comment has been minimized.

Show comment
Hide comment
@mitar

mitar Jul 19, 2017

Collaborator

This is not a reproduction. Please create a small Meteor app in a git repo and provide a link to it here. Because the only thing I can tell you based on your snippet above is: it works for me.

Collaborator

mitar commented Jul 19, 2017

This is not a reproduction. Please create a small Meteor app in a git repo and provide a link to it here. Because the only thing I can tell you based on your snippet above is: it works for me.

@wei170

This comment has been minimized.

Show comment
Hide comment
@wei170

wei170 Jul 19, 2017

Oh, I figured out. It was caused by templating-compiler version. I was on @1.2.15. When I updated it to @1.3.2, it works.

wei170 commented Jul 19, 2017

Oh, I figured out. It was caused by templating-compiler version. I was on @1.2.15. When I updated it to @1.3.2, it works.

@Roshdy

This comment has been minimized.

Show comment
Hide comment
@Roshdy

Roshdy Sep 9, 2017

Can you please add this fix to the documentation of blaze

Roshdy commented Sep 9, 2017

Can you please add this fix to the documentation of blaze

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