Permalink
Browse files

Adding more tilt examples

  • Loading branch information...
1 parent f447f39 commit ea28f44b2014b83a89f43b4bf15d606d4b17ab63 @nathanbain committed Jan 31, 2012
Showing with 49 additions and 15 deletions.
  1. +18 −15 lib/contact_details.rb
  2. +24 −0 lib/episode_generator.rb
  3. +7 −0 templates/episodes.erb
View
@@ -1,20 +1,23 @@
require 'tilt'
-
-contacts = {
+
+class Contacts
+
+ contacts = {
"Email" => "nathan_bain@hotmail.com",
"Twitter" => "@nathanbain",
"Facebook" => "nathan.bain",
"Skype" => "nathanbainitv" }
-
-context = Object.new
-def context.title
- "Contact Details"
-end
-
-template = Tilt.new("templates/contact_list.erb")
-
-File.open "output/contact_me.html", "w" do |file|
- file.write template.render(context) {
- Tilt.new("templates/sites.erb").render(Object.new, contacts: contacts)
- }
-end
+
+ context = Object.new
+ def context.title
+ "Contact Details"
+ end
+
+ template = Tilt.new("templates/contact_list.erb")
+
+ File.open "output/contact_me.html", "w" do |file|
+ file.write template.render(context) {
+ Tilt.new("templates/sites.erb").render(Object.new, contacts: contacts)
+ }
+ end
+ end
View
@@ -0,0 +1,24 @@
+require 'erb'
+require 'tilt'
+require 'json'
+
+class EpisodeGenerator
+ template = Tilt.new('templates/episodes.erb')
+
+ episodes = '[{"id":"1","name":"Episode 1","length":"1h 02m"},{"id":"2","name":"Episode 2","length":"1h 03m"},{"id":"3","name":"Episode 3","length":"1h 04m"}]'
+
+ ratings = '[{"id":"1","rating":"3/5"},{"id":"2","rating":"4/5"},{"id":"3","rating":"5/5"}]'
+
+ json_episodes = JSON.parse(episodes)
+ json_ratings = JSON.parse(ratings)
+ episodes_with_ratings = []
+
+ json_episodes.each do |e|
+ rating = json_ratings.select { |r| r["id"] == e["id"]}
+ item = Hash["id", e["id"], "name", e["name"], "length", e["length"], "rating", rating[0]["rating"]]
+ episodes_with_ratings << item
+ end
+
+ output = template.render(Object.new, episodes:episodes_with_ratings)
+ puts output
+end
View
@@ -0,0 +1,7 @@
+<% episodes.each do |episode| %>
+ ID: <%= episode["id"] %>
+ Name: <%= episode["name"] %>
+ Length: <%= episode["length"] %>
+ Rating: <%= episode["rating"] %>
+<% end %>
+

0 comments on commit ea28f44

Please sign in to comment.