machty / emblem.js

Emblem.js - Ember-friendly, indented syntax alternative for Handlebars.js
http://emblemjs.com
MIT License
1.04k stars 81 forks source link

Link-to helper doesn't like class conditions #145

Closed dodeja closed 5 years ago

dodeja commented 10 years ago

I am trying to use class conditions with a link-to helper, but fails on compilation.

link-to view.linkName lnid urlSlug itemprop="url" class={view.isSelected:selected view.icon} doesnt work.

link-to view.linkName lnid urlSlug itemprop="url" class=view.isSelected:selected doesnt work either.

a href="#" class={view.isSelected:selected view.icon} works fine.

machty commented 10 years ago

How would you write what you're trying to do in Handlebars?

emaillenin commented 10 years ago

+1

Unable to assign class name to a Anchor tag generated by link-to helper.

Handlebar way of doing it (http://stackoverflow.com/questions/14552911/add-class-to-ember-link-to)

{{#link-to "rent" rent classNames="btn btn-primary btn-small"}}Go to rent{{/link-to}}

Tried to do it in emblem.js like this (none worked):

link-to.class-name link-to class=class-name link-to (class=class-name) link-to {class=class-name}

bnetter commented 10 years ago

Same problem here :+1:

krezka commented 10 years ago

Without condition you can easily add class, after link-to (space is required). Look here: https://github.com/machty/emblem.js/issues/73 Example: link-to .class-name rent rent

tmosmant commented 9 years ago

+1 a fix would be much appreciated.

renatocarvalho commented 9 years ago

+1

Leooo commented 9 years ago

+1

Leooo commented 9 years ago

the following works (with test a CP defined in the controller):

= link-to 'authenticated.profile' curProfile.slug classNameBindings="test" class="greenable_pill"
  | Your profile

But I can not manage to use an inline helper: this doesn't work

= link-to 'authenticated.profile' curProfile.slug classNameBindings="if true 'active_tutorial' ''" class="greenable_pill"
  | Your profile

neither does classNameBindings="{{if true 'active_tutorial' ''}]" nor classNameBindings=(if true 'active_tutorial' '') nor `class="{{if true 'active_tutorial' ''}}"

thec0keman commented 9 years ago

Inside mustache you need to use Ember's inline if:

= link-to linkName lnid urlSlug itemprop="url" class=(if isSelected selected)
=>
{{link-to linkName lnid urlSlug itemprop="url" class=(if isSelected selected)}}

Some things to keep in mind:

a class=isActive:active:not   =>   <a class={{if isActive 'active' 'not'}}></a>

I'm pretty sure the old colon syntax of isActive:active:inactive is not supported anymore by Ember.

Leooo commented 9 years ago

ok got it thanks, class=(if ..) works, but then I can't add fixed classes so I have to do someting like that:

  = link-to 'authenticated.profile' curProfile.slug class=(if true 'active_tutorial tutorial-hover-own tutorial-hover-own-home greenable_pill' 'tutorial-hover-own tutorial-hover-own-home greenable_pill')
    | Your profile`

A bit cumbersome, but it works. -1

thec0keman commented 9 years ago

What if you mixed class and classNames?

= link-to 'admin' class='tutorial-hover-own tutorial-hover-own-home greenable_pill' classNames=(if isActive 'active_tutorial')

I'm pretty sure classNames is the preferred path, but that should still work.

Leooo commented 9 years ago

I had an assertion failed error with your proposal (~ Only array of static class names allowed for classNames), but putting the dynamic classes into class= works, thanks:

  = link-to 'admin' classNames='tutorial-hover-own tutorial-hover-own-home greenable_pill' class=(if isActive 'active_tutorial')
kandebonfim commented 8 years ago

+1 please. OCD coder here!

kandebonfim commented 8 years ago
link-to .navtest 'dashboard' | Click me

Worked for me!

But I detected one more issue related to that: https://github.com/machty/emblem.js/issues/282

thec0keman commented 8 years ago

One thing to help debug these sorts of issues is to first figure out what the handlebars should look like, so you can determine if it is Ember upset with the syntax or if Emblem is doing something wrong.

I believe what we want is the following:

{{#link-to 'admin' classNames='tutorial-hover-own tutorial-hover-own-home greenable_pill' class=(if isActive 'active_tutorial')}}test{{/link-to}}

That does appear to work correctly in Ember.

I did some tests on the latest Emblem, and the following work:

 = link-to 'test' 'admin' classNames='tutorial-hover-own tutorial-hover-own-home greenable_pill' class=(if isActive 'active_tutorial')
= link-to 'admin' classNames='tutorial-hover-own tutorial-hover-own-home greenable_pill' class=(if isActive 'active_tutorial')
  |test   

However this appears to be broken:

= link-to 'admin' classNames='tutorial-hover-own tutorial-hover-own-home greenable_pill' class=(if isActive 'active_tutorial') test   

I'll take a look at why it is getting confused in the latter case. Does this line up with your experience?

thec0keman commented 8 years ago

Actually, that latter case is expected. Components / helpers should be able to receive single arguments, and so without a | or a new-line there is no way to know when the helpers parameters end.

@Leooo @kandebonfim could you confirm if the above examples work for you? Or is there a variation that is not working?

Thanks!