Ruby/Rails/ERB

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

A loop executes the body of the loop

 
<html>
  <head>
    <title>Using Views</title>
  </head>
  <body>
    <br>
    <% 3.times do %>
    Yes! <br><p>line</p>
    <% end %>
  </body>
</html>


<A href="http://www.crossplatform.ru/Code/RubyDownload/loopWithHTMLBody.zip">loopWithHTMLBody.zip( 88 k)</a>


Escaping Sensitive Text

    <br>
    <%= h("I say that here<there alphabetically.") %>
    <br>


<A href="http://www.crossplatform.ru/Code/RubyDownload/htmlEscape.zip">htmlEscape.zip( 87 k)</a>


In Rails, embedded Ruby in .rhtml pages is run using a processor called ERb, for Embedded Ruby.

<html>
  <head>
    <title>Using Ruby on Rails</title>
  </head>
  <body>
    <%= 2 + 3 %>
  </body>
</html>


<A href="http://www.crossplatform.ru/Code/RubyDownload/firstERB.zip">firstERB.zip( 87 k)</a>


Mix Ruby Code and HTML

<html>
  <head>
    <title>Using Views</title>
  </head>
  <body>
    <h1>Working With Views</h1>
    This is an active view in a Ruby on Rails application.
    <br>
    <br>
    2 + 3 = <%= 2 + 3 %>
    <br>
    <br>
    Do loops work?
    <br>
    <% 3.times do %>
    Yes! <br>
    <% end %>
    <br>
    This page executes Ruby code on-the-fly.
  </body>
</html>


<A href="http://www.crossplatform.ru/Code/RubyDownload/mixHTMLRuby.zip">mixHTMLRuby.zip( 88 k)</a>


use puts to display text in a web page (It was printed out in the console.)

<html>
  <head>
    <title>Using Views</title>
  </head>
  <body>
    <h1>Working With Views</h1>
    This is an active view in a Ruby on Rails application.
    <br>
    <br>
    2 + 3 = <%= 2 + 3 %>
    <br>
    <% puts "Hi" %>
    <br>
    This page executes Ruby code on the fly.
  </body>
</html>


<A href="http://www.crossplatform.ru/Code/RubyDownload/usePuts.zip">usePuts.zip( 87 k)</a>


uses the Ruby Time.now method to display the current time

<html>
  <head>
    <title>Using Views</title>
  </head>
  <body>
    The time is now <%= Time.now %>
  </body>
 </html>


<A href="http://www.crossplatform.ru/Code/RubyDownload/displayCurrentTime.zip">displayCurrentTime.zip( 89 k)</a>


You can mix HTML and ERb as well

<html>
  <head>
    <title>Using Ruby on Rails</title>
  </head>
  <body>
    <br>
    2 + 3 = <%= 2 + 3 %>
    <br>
  </body>
</html>


<A href="http://www.crossplatform.ru/Code/RubyDownload/mixHTMLERB.zip">mixHTMLERB.zip( 87 k)</a>