0

I have a piece of html text that includes some ruby variables, and depends on a loop with unknown number of iterations. What I have now (and works):

test.rb (piece)

head = <<-HEAD
  <html>
    <head>
      <title>Title</title>
      <link rel="stylesheet" type="text/css" href="style.css">
      <script src="scripts.js" type="text/javascript"></script>
    </head>
    <body>
  HEAD

body = '<div class="flex-container">'

items.each do |i|
  body << %(
    <div class="container">
      <button id='action#{i.ref}'
        style="background-color: 'inherit';"
        onmouseover="this.id='action'; this.style.background='highlight';"
        onclick="selectItem()"
        onmouseout="this.id='action#{i.ref}'; this.style.background='inherit';"
        value="#{i.root}">
      <img src="#{i.image}" alt="#{i.name}_img">
      <div>#{i.name}</div>
      </button>
    </div>
  )
end

body += '</div></body></html>'
html = head + body

That is the way I generate my html text. I want to have a separate html file and somehow link what's in the body to the ruby code of last example, i.e.:

test.html

<html>
  <head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="scripts.js" type="text/javascript"></script>
  </head>
  <body>
    <div class="flex-container">
       <!-- HERE HAVE SOME LINK TO THE BODY OF test.rb -->
    </div>
  </body>
</html>

Any hint? Thanks.

1 Answer 1

1

Your file test.html should have extention 'html.erb'

test.html.erb

<html>
  <head>
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <script src="scripts.js" type="text/javascript"></script>
  </head>
  <body>
    <div class="flex-container">
       <%= @body %>
    </div>
  </body>
</html>

In test.rb file you should declare instance variable, in this way:

@body += '</div></body></html>'
Sign up to request clarification or add additional context in comments.

1 Comment

That works. Just to say in case of using Ruby and not Ruby on Rails, the ERB gem is needed. Good to know, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.