polymer icon indicating copy to clipboard operation
polymer copied to clipboard

Feature request: <dom-if-else>

Open nazar-pc opened this issue 11 years ago • 44 comments

Very often I encounter that I need not only if, but also else, while combination of two ifs is quite ugly and violates DRY principle. I suggest to add <dom-if-else> for such cases. Namely, instead of:

<template is="dom-if" if="{{condition}}">
    <p>P1</p>
</template>
<template is="dom-if" if="{{!condition}}">
    <p>P2</p>
</template>

I'd like to have:

<template is="dom-if-else" if="{{condition}}">
    <p>P1</p>
    <p>P2</p>
</template>

More complex cases can be handled in such way:

<template is="dom-if-else" if="{{condition}}">
    <template>
        <p>P1</p>
        <p>sub-P1</p>
    </template>
    <template>
        <p>P2</p>
        <p>sub-P2</p>
    </template>
</template>

<template> or any other element can be used to wrap more than one element, rule is simple:

  • [is=dom-if-else] > :nth-child(1): if branch
  • [is=dom-if-else] > :nth-child(2): else branch

nazar-pc avatar Jul 28 '15 23:07 nazar-pc

+1

redjar avatar Aug 18 '15 07:08 redjar

See: https://github.com/Trakkasure/dom-else

Trakkasure avatar Aug 18 '15 19:08 Trakkasure

@Trakkasure, I remember it, probably, from Slack channel, but it doesn't have as compact syntax as described here. It does allow to avoid condition duplication, but still very verbose in simple cases.

nazar-pc avatar Aug 18 '15 19:08 nazar-pc

Ok. You asked for it.. so here it is: https://github.com/Trakkasure/dom-if-else

Trakkasure avatar Aug 18 '15 21:08 Trakkasure

@Trakkasure Thanks!

redjar avatar Aug 19 '15 06:08 redjar

Just tried to use it today. Doesn't work well in Polymer 1.0 at least.

redjar avatar Aug 19 '15 14:08 redjar

I integrated a bunch of tests from dom-if.... Let me know what is going wrong so I can test better.

Trakkasure avatar Aug 19 '15 18:08 Trakkasure

Leave issues on the dom-if-else repo: https://github.com/Trakkasure/dom-if-else/issues

Trakkasure avatar Aug 19 '15 18:08 Trakkasure

Tests added to it.

Trakkasure avatar Aug 21 '15 19:08 Trakkasure

:+1:

tomalec avatar Sep 20 '15 23:09 tomalec

+1

GeoloeG-IsT avatar Nov 12 '15 14:11 GeoloeG-IsT

What about this kind of syntax where "else if" could be introduced:

<template is="dom-if" if="{{condition1}}">
    <p>P1</p>
</template>
<template is="dom-else-if" else-if="{{condition2}}">
    <p>P2</p>
</template>
<template is="dom-else">
    <p>P3</p>
</template>

GeoloeG-IsT avatar Nov 12 '15 14:11 GeoloeG-IsT

this is no different than negating the original condition with another dom-if template.

On Thu, Nov 12, 2015 at 8:31 AM, Pascal Gula [email protected] wrote:

What about this kind of syntax where "else if" could be introduced:

— Reply to this email directly or view it on GitHub https://github.com/Polymer/polymer/issues/2165#issuecomment-156116396.

Trakkasure avatar Dec 04 '15 14:12 Trakkasure

+1

kunhimohamed avatar Dec 30 '15 06:12 kunhimohamed

+1 On Dec 29, 2015 10:28 PM, "kunhimohamed" [email protected] wrote:

+1

— Reply to this email directly or view it on GitHub https://github.com/Polymer/polymer/issues/2165#issuecomment-167944904.

[image: CHI Logo]Cultural Homestay International (CHI)Toll Free: 1-800-432-4643Website: chinet.orgCHI Blog https://chinet.org/blog/ | Facebook https://www.facebook.com/CulturalHomestayInternational | YouTube https://www.youtube.com/user/chinetorg | Twitter https://twitter.com/CHI_News

chimarcos avatar Dec 30 '15 07:12 chimarcos

+1

fabianbormann avatar Jan 04 '16 09:01 fabianbormann

+1

allenmoatallen avatar Jan 19 '16 21:01 allenmoatallen

+1

krozycki avatar Jan 27 '16 16:01 krozycki

+1

Warrenty avatar Mar 23 '16 22:03 Warrenty

+1

kzahel avatar May 06 '16 23:05 kzahel