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

  • Home
  • SEARCH
  • 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 6103973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:47:22+00:00 2026-05-23T13:47:22+00:00

In my CMS application I use gsub to replace parts of a user supplied

  • 0

In my CMS application I use gsub to replace parts of a user supplied html template (string) with dynamic code from the application (such as a comment from a user, or page content). I’m upgrading from Rails 2.3.5 to 2.3.12 in preparation for a move to Rails 3 and have installed the rails_xss gem to try and get my strings sorted.

I’m wondering if there’s a way to gsub strings into another string (the template), treating the incoming string as unsafe, but the template sections as safe?

I already sense that I’m going to have to rethink this and perhaps split the template around my areas of concern and insert my dynamic bits in the gap, passing the whole thing out as an array of various safe and unsafe strings to be rendered. Does that sound like the sane way to go.

UPDATE

Here is an example of what I currently do. It’s in a helper that gets called from a layout. The theme is fetched from the database. It’s just a string with specific HTML Comments that we’re looking to replace with the actual content:

final_theme.gsub!('<body>', '<body>' + (render '/user_bars/user_bar'))
final_theme.gsub!('</body>', (render '/theme/google_analytics') + '</body>') if SiteSetting.first.google_analytics_code.present?
final_theme.gsub!('<!--THEME_MetaTitle-->', (render '/theme/meta_title'))
final_theme.gsub!('<!--THEME_Breadcrumbs-->', (render '/theme/breadcrumbs'))
final_theme.gsub!('<!--THEME_Footer-->', (render '/theme/footer'))
final_theme.gsub!('<!--THEME_Body-->', (render '/theme/body'))

Those are simple cases, I also have comments that have actual parameters in them (for example to specify how many levels deep to draw an index bar, of which there could be many on a page). I use scan to find the details and then gsub again to replace the comment with the actual indexbar code.

I’m totally open to any refactoring advice as this is an area of my code that doesn’t satisfy me at all.

  • 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-05-23T13:47:22+00:00Added an answer on May 23, 2026 at 1:47 pm

    After installing the latest plugin from rails/rails_xss (rather than NZKoz’s one) I started getting errors saying gsubing a SafeBuffer was not allowed. Fair enough 🙂

    I’ve come up with this as an alternative and will accept this as the correct answer if someone can vouch for its correctness:

    final_theme = final_theme.html_safe
    
    final_theme = final_theme.split('<!--THEME_Breadcrumbs-->').inject do |output, piece| 
      output + render('/theme/breadcrumbs') + piece
    end
    

    That’s just an extract but we’re basically marking the original theme as safe (because it is) and then splitting on the piece of interest, injecting our new code into the mix by concatenating the strings together, thus ensuring html_safeness!

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

Sidebar

Related Questions

The CMS I'm using allows me to access the user's time offset from UTC/GMT
i'm trying to upgrade an old CMS to use NHibernate and can't deter from
I am trying to use the Database mailer extension for my Radiant CMS application.
I have an application with a Java back-end. I wanted to use Drupal CMS
This is the template language of Stacy (a super light HTML/PHP CMS that doesn't
we are trying to refactor an JSP/XSLT application to JSF. We use a CMS
What do you think of the project structure for an opensource CMS application built
I'm working on an ASP.NET WebForms CMS application, and having a bit of trouble
I am using Spring 3, JPA + Hibernate for a CMS application. In that
I'm currently involved with developing a C# CMS-like web application which will be used

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.