Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'rubygems'
- $LOAD_PATH.unshift File.dirname(__FILE__) + '/lib'
- require 'sinatra'
- require 'erubis'
- require 'sinatra/static_assets'
- set :erubis, :pattern => '\{% %\}', :trim => true
- require 'rdiscount'
- set :markdown, :layout => false
- helpers do
- # Capture a block of content to be rendered later:
- #
- # {% content_for :head do %}
- # <script type="text/javascript" src="/js/deadline.js"></script>
- # {% end %}
- def content_for(key, &block)
- content_blocks[key.to_sym] << block
- end
- def content_for?(key)
- content_blocks.has_key?(key.to_sym)
- end
- def content_blocks
- @content_blocks ||= Hash.new {|h,k| h[k] = [] }
- end
- # Render the captured block:
- #
- # <head>{%= output_content_for :head %}</head>
- #
- # This passes <tt>1</tt> and <tt>2</tt> to the block
- # registered for <tt>:head</tt>.
- def output_content_for(key)
- content_blocks[key.to_sym].map do |content|
- content.call
- end.join
- end
- # Use the captured block:
- #
- # <head>
- # <title><%= content_for?(:title) ? output_content_for(:title) : "Untitled" %></title>
- # </head>
- def title(page_title, show_title = true)
- content_for(:title) { page_title.to_s }
- @show_title = show_title
- end
- def show_title?
- @show_title
- end
- # Use in the head:
- #
- # <head>{%= output_content_for(:head) %}</head>
- def stylesheets(*args)
- content_for(:head) { stylesheet_link_tag(*args) }
- end
- def javascripts(*args)
- content_for(:head) { javascript_script_tag(*args) }
- end
- end
- get '/about' do
- "I'm running version " + Sinatra::VERSION
- end
- get '/' do
- erubis markdown(:index)
- end
- __END__
- @@layout
- <meta charset="utf-8" />
- <title>{%= content_for?(:title) ? output_content_for(:title) : "Untitled" %}</title>
- {%= output_content_for(:head) -%}
- <body>
- {% if content_for?(:title) && show_title? %}
- <h1>{%= output_content_for(:title) %}</h1>
- {% end %}
- {%= yield %}
- @@ index
- {% javascripts "/js/application.js" %}
- {% stylesheets "/css/application.css" %}
- {% title "Hello Markdown!" %}
- Sinatra & Tilt are awesome!
Add Comment
Please, Sign In to add comment