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

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

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

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

ramezrafla opened this issue May 13, 2016 · 33 comments
Labels
Milestone

Comments

@ramezrafla
Copy link

@ramezrafla ramezrafla commented May 13, 2016

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 {{elsif}} {{elsif}} or {{elseif}} or {{#elseif}} May 13, 2016
@mitar
Copy link
Collaborator

@mitar mitar commented May 13, 2016

Does handlebars have it?

@mitar
Copy link
Collaborator

@mitar 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
Copy link
Author

@ramezrafla 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
Copy link
Collaborator

@mitar 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
Copy link
Author

@ramezrafla ramezrafla commented May 13, 2016

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

@AdrienTorris
Copy link

@AdrienTorris AdrienTorris commented Jul 22, 2016

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

akanix42 added a commit to akanix42/blaze that referenced this issue Jul 28, 2016
@akanix42
Copy link

@akanix42 akanix42 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
Copy link
Author

@ramezrafla ramezrafla commented Jul 28, 2016

Excellent @nathantreid,
Can you PR with your changes?

@akanix42
Copy link

@akanix42 akanix42 commented Jul 28, 2016

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

@ramezrafla
Copy link
Author

@ramezrafla ramezrafla commented Jul 28, 2016

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

@x5engine
Copy link

@x5engine x5engine commented Aug 19, 2016

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

@akanix42
Copy link

@akanix42 akanix42 commented 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.

@ramezrafla
Copy link
Author

@ramezrafla ramezrafla commented Dec 16, 2016

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

@mitar
Copy link
Collaborator

@mitar 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
Copy link
Author

@ramezrafla 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
Copy link
Collaborator

@mitar 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
Copy link
Collaborator

@mitar mitar commented Jan 12, 2017

Released as 2.3.0. You have to update templating package.

@namirsab
Copy link

@namirsab 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
Copy link
Collaborator

@mitar mitar commented May 17, 2017

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

@namirsab
Copy link

@namirsab namirsab commented May 17, 2017

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

@maxfi
Copy link

@maxfi 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
Copy link
Collaborator

@mitar mitar commented Jun 11, 2017

@maxfi
Copy link

@maxfi maxfi commented Jun 18, 2017

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

@mitar
Copy link
Collaborator

@mitar mitar commented Jun 18, 2017

Feel free to make a pull request. :-)

@wei170
Copy link

@wei170 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
Copy link
Collaborator

@mitar mitar commented Jul 19, 2017

Which version of spacebars-compiler package you have?

@wei170
Copy link

@wei170 wei170 commented Jul 19, 2017

My spacebars-compiler is @1.1.2

@mitar
Copy link
Collaborator

@mitar mitar commented Jul 19, 2017

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

@wei170
Copy link

@wei170 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
Copy link
Collaborator

@mitar 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
Copy link

@wei170 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
Copy link

@Roshdy 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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
9 participants
You can’t perform that action at this time.