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 8948821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:05:32+00:00 2026-06-15T13:05:32+00:00

Please note this is not a question asking for any working solution, but a

  • 0

Please note this is not a question asking for any working solution, but a question of a what is the best practice? kind.

Throughout my views I have a lot of occurrences of the code like this (I use HAML):

= t("some.key")
%b= t("other.key.#{string}")
%i= string

As soon as it is used in many different places I decided to extract it to a helper. But I realized that I do not understand what is the cleanest way to do it.

I can think of using string concatenation (+), or string formatting ("%s %s %s" % [...]), or array join ([...].join("\n")) but all of these methods look a bit excessive for me, because I have to build some other objects (strings, arrays) or specify delimiters(" " or "\n") and bothering with html_safe instead of just declaring three string.

I thought that concat is what is supposed to work this way

def long_string(string)
  concat t("some.key")
  concat content_tag(:b, t("other.key.#{string}"))
  concat content_tag(:i, t(string)
end

… and it works, but it forces me to execute the helper instead of evaluating it, which is not idiomatic, I mean to use

%p- long_string(string)

instead of

%p= long_string(string)

Of course I can extract it to a partial, but it seems to much for a code of three lines.

That said, am I missing some clean and elegant way to concatenate three HTML-enriched lines in a helper, or concat/partial/dirty array joins are my only options?

  • 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. Editorial Team
    Editorial Team
    2026-06-15T13:05:33+00:00Added an answer on June 15, 2026 at 1:05 pm

    If it is a lot of HTML I would definitely go for extracting the partial. Nothing writes and reads HTML cleaner than HAML in my personal opinion.

    If it is a bunch of string interpolation, composing a lot of values, I sometimes use helpers to clean up my view, but they would always return strings, I never use concat in my helpers, this makes my helpers better testable as well (just check if the string result is what I expect it to be).

    So, for your example it would be

    def long_string(str)
      result = []
      result << t("some.key")
      result << content_tag(:b, t("other.key.#{string}"))
      result << content_tag(:i, string)
      result.join(" ").html_safe
    end
    

    and in your view you can just write

    %p= long_string('whatevvva')
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please note this is not a question about online/hosted SVN services. I am working
Please note that this is asking a question about constructors, not about classes which
Please note: This is a question about the Eclipse plugin Subversive , and not
PLEASE NOTE: This is a CODE QUESTION NOT AN SYSTEM MANIPULATION ONE. I have
Please note this question is specific to WCF Data Services not normal Wcf Service.
Please note that this is not homework and i did search before starting this
Note: Not sure if this is the right stack, please tell if I should
Please note this question related to performance only. Lets skip design guidelines, philosophy, compatibility,
[Please note that this is a different question from the already answered How to
Please note that this question is from 2008 and now is of only historic

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.