Skip to content

Instantly share code, notes, and snippets.

@scmx
Created January 31, 2014 11:46
Show Gist options
  • Save scmx/8730623 to your computer and use it in GitHub Desktop.
Save scmx/8730623 to your computer and use it in GitHub Desktop.
ERB example

Ruby ERB example

# Kompilera erb-filen till html
ruby example.rb

# Visa resultatet i webbläsaren
open index.html
require 'erb'
class Post
attr_accessor :title, :body
@@posts = []
def initialize(title: nil, body: nil)
@title = title
@body = body
end
def publish
@@posts.push(self)
end
def self.all
@@posts
end
end
Post.new(title: 'Hello World', body: 'Lorem Ipsum').publish
Post.new(title: 'Welcome', body: 'To ruby').publish
renderer = ERB.new(File.read('index.html.erb'))
File.write('index.html', renderer.result)
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<h1>Blogg</h1>
<p>
Tiden är nu: <%= Time.now %>
</p>
<% Post.all.each do |post| %>
<article>
<h2><%= post.title %></h2>
<div><%= post.body %></div>
</article>
<% end %>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment