Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 175191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:39:19+00:00 2026-05-11T13:39:19+00:00

In every webapp I build, I come across this issue. I want to set

  • 0

In every webapp I build, I come across this issue. I want to set classes on HTML elements conditionally. For example, sometimes the <div> for a post looks like:

<div class='post'>...</div> 

And sometimes it looks like this:

<div class='post even recent replied_to author_is_admin'>...</div> 

Each class after post is there because some logic decided it should be. What is the best way to this? In Rails+HAML, I’ve been guilty of doing stuff like:

-classes = [] -classes << cycle('even', 'odd') -classes << 'recent' if post.recent? -classes << 'replied_to' if post.replied_to? -classes << 'author_is_admin' if post.author_is_admin? .post{:class => classes.join(' ')}     ... 

It’s not pretty, and I’ve shortened it to use a helper so I can do this:

.post{:class => '#{cycle('even', 'odd')} #{post_classes}'} 

It still seems like it should be easier to read, because it’s something we do all the time. Does you have any methods by which you make this process short and readable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T13:39:20+00:00Added an answer on May 11, 2026 at 1:39 pm

    I think what you’ve done is fine. I would only suggest a few minor improvements. One would be to put the cycle call in post_classes. For readability, I would make post_classes accept Post argument. This creates a little redundancy, but you can easily avoid it by defining this helper method:

    def classes(object)   case object   when Post then post_classes(object)   end end 

    That way, your template code will look like this:

    .post{:class => classes(Post)} 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm planning to a webapp where every guy using it would have a client
Every example I find of anyone implementing plugins in android seems to use aexp.intent.action.
Every time i reset & seed my database it wipes out the standard admin@example.com
Every example I've seen on how to deploy an MVC application to Windows Azure
I'm using Eclipse and Ant in my development environment. I've come across a problem
I'd like to build a webapp in Rails in which I'd like to determine
As far as I see every time I make a change, for example the
In my settings.gradle file, I originally had this which works include 'server', 'webapp' I
I'm trying to build a very, very simple micro-webapp which I suspect will be
For one of my ASP.NET 3.5 applications, every single time I try to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.