# Kompilera erb-filen till html
ruby example.rb
# Visa resultatet i webbläsaren
open index.html
Created
January 31, 2014 11:46
-
-
Save scmx/8730623 to your computer and use it in GitHub Desktop.
ERB example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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