Guest User

Untitled

a guest
Jul 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. require 'rubygems'
  2.  
  3. $LOAD_PATH.unshift File.dirname(__FILE__) + '/lib'
  4.  
  5. require 'sinatra'
  6. require 'erubis'
  7.  
  8. require 'sinatra/static_assets'
  9.  
  10. set :erubis, :pattern => '\{% %\}', :trim => true
  11.  
  12. require 'rdiscount'
  13.  
  14. set :markdown, :layout => false
  15.  
  16. helpers do
  17. # Capture a block of content to be rendered later:
  18. #
  19. # {% content_for :head do %}
  20. # <script type="text/javascript" src="/js/deadline.js"></script>
  21. # {% end %}
  22. def content_for(key, &block)
  23. content_blocks[key.to_sym] << block
  24. end
  25.  
  26. def content_for?(key)
  27. content_blocks.has_key?(key.to_sym)
  28. end
  29.  
  30. def content_blocks
  31. @content_blocks ||= Hash.new {|h,k| h[k] = [] }
  32. end
  33.  
  34. # Render the captured block:
  35. #
  36. # <head>{%= output_content_for :head %}</head>
  37. #
  38. # This passes <tt>1</tt> and <tt>2</tt> to the block
  39. # registered for <tt>:head</tt>.
  40. def output_content_for(key)
  41. content_blocks[key.to_sym].map do |content|
  42. content.call
  43. end.join
  44. end
  45.  
  46. # Use the captured block:
  47. #
  48. # <head>
  49. # <title><%= content_for?(:title) ? output_content_for(:title) : "Untitled" %></title>
  50. # </head>
  51. def title(page_title, show_title = true)
  52. content_for(:title) { page_title.to_s }
  53. @show_title = show_title
  54. end
  55.  
  56. def show_title?
  57. @show_title
  58. end
  59.  
  60. # Use in the head:
  61. #
  62. # <head>{%= output_content_for(:head) %}</head>
  63. def stylesheets(*args)
  64. content_for(:head) { stylesheet_link_tag(*args) }
  65. end
  66.  
  67. def javascripts(*args)
  68. content_for(:head) { javascript_script_tag(*args) }
  69. end
  70. end
  71.  
  72. get '/about' do
  73. "I'm running version " + Sinatra::VERSION
  74. end
  75.  
  76. get '/' do
  77. erubis markdown(:index)
  78. end
  79.  
  80. __END__
  81.  
  82. @@layout
  83. <meta charset="utf-8" />
  84. <title>{%= content_for?(:title) ? output_content_for(:title) : "Untitled" %}</title>
  85. {%= output_content_for(:head) -%}
  86. <body>
  87. {% if content_for?(:title) && show_title? %}
  88. <h1>{%= output_content_for(:title) %}</h1>
  89. {% end %}
  90. {%= yield %}
  91.  
  92. @@ index
  93. {% javascripts "/js/application.js" %}
  94. {% stylesheets "/css/application.css" %}
  95. {% title "Hello Markdown!" %}
  96. Sinatra & Tilt are awesome!
Add Comment
Please, Sign In to add comment