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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:07:43+00:00 2026-05-12T15:07:43+00:00

So I have a have a solution for this, but I wanted to get

  • 0

So I have a have a solution for this, but I wanted to get some opinions and see if there was a better way to do this.

My problem is basically that I list of data that will get generated on load by RoR and then have JS controls to view subsets. I would rather not have to support two versions of what is basically the same HMTL. So my solution is this, I have a ruby partial that looks something like this:

<% value1 = jsmode ? '#{value1}' : object.value1
value2 = jsmode ? '#{value2}' : object.value2 $>
<div class="object_template">
<div><$= value1 $></div>
<div><%= value2 %></div>
</div>

So when I render the partial with ruby, I pass in a jsmode of false and it renders like a normal partial. Then I will render the partial again wrapped with a div with an ID of template but this time with a jsmode of true. This will render the html with the #{} strings. I can then pass the template to prototype Template object like this:

new Template($('template')).evaluate({value1: '111', value2: '222'});

So my question is, is there a better way to do this?

I know that I can use the #{} construct to pass variables to RoR if I wrap them with double quotes, but I need the #{} to render for the JS template.

Thoughts?

  • 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-12T15:07:43+00:00Added an answer on May 12, 2026 at 3:07 pm

    The general approach looks good and actually probably a little better than what I’ve done before in some cases.

    However, it looks to me like you’re actually rendering the JS version to a div and then passing that into Prototype Template. Template also accepts a string so you can just pass the raw js template straight to it. Unless I’m missing something, the #template div is not necessary on its own. So the string route is probably the better way to go.

    From Rails, it would probably look something like this:

    <script type="text/javascript">
      var raw_template = "#{escape_javascript(render(:partial => 'template', :locals => { :jsmode => true }))}";
      var output = new Template(raw_template).evaluate({value1: '111', value2: '222'});
      // Do something with the output
    </script>
    

    escape_javascript uses the following map:

    JS_ESCAPE_MAP = {
      '\\'    => '\\\\',
      '</'    => '<\/',
      "\r\n"  => '\n',
      "\n"    => '\n',
      "\r"    => '\n',
      '"'     => '\\"',
      "'"     => "\\'" }
    

    This means that given your suggested template, you should end up with a result along the lines of:

     var raw_template = "<div class=\"object_template\">\n<div>#{value1}<\/div>\n<div>#{value2}<\/div>\n<\/div>";
    

    Which should do the trick nicely.

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

Sidebar

Related Questions

I've searched google and SO for a solution to this problem but have not
This is a weird problem. I have a solution for it, but I don't
This may have been answered before, but I cannot find a solution that works.
I have searched for a general solution to this but only find answers to
I have been trying to find a solution to this one but could not
I have seen the solution to this a while back, but can't seem to
I have read several forums but can't find the solution to this. int sIndex
I used the mapping solution from this question to have a joined component. But
I've tried to search for a solution for this, but have had no joy:
Alright, this one's interesting. I have a solution, but I don't like it. The

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.