how can i use if to do this things? #648

Closed
by5739 opened this Issue May 20, 2012 · 2 comments

Comments

Projects
None yet
2 participants

by5739 commented May 20, 2012

my perpose is to if menu == 'active', then ouput:

li.active
    a(href='xx') link

otherwise output :

li
    a(href='xx') link

so i must write the code like this:

if menu == 'active'
    li.active
        a(href='xx') link
else
    li
        a(href='xx') link

as you see, this statement "a(href='xx') link" is duplicate, so i want to write the code like this:

if menu == 'active'
    li.active
else
    li
        a(href='xx') link

but it doesn't work , i think if this statement "a(href='xx') link" has some other logic, i must repeat it in "if" block and "else" block again and again???...i must copy the same code from this place to other place, i don't think it's a good idea, how can i do?

Contributor

tj commented May 20, 2012

li(class=active ? 'active : '') link`

or something DRY like:

active = 'Contact'

mixin menu(title, path)
  li(class=active == title ? 'active' : '')
    a(href=path)= title

ul
  +menu('Home', '/')
  +menu('About', '/about')
  +menu('Contact', '/contact')

outputting:

<ul>
  <li class=""><a href="/">Home</a></li>
  <li class=""><a href="/about">About</a></li>
  <li class="active"><a href="/contact">Contact</a></li>
</ul>

by5739 commented May 20, 2012

thank you very much!!! it's so nice

by5739 closed this May 20, 2012

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