Skip to content

Instantly share code, notes, and snippets.

@wbzyl
Created December 21, 2010 18:25
Show Gist options
  • Save wbzyl/750329 to your computer and use it in GitHub Desktop.
Save wbzyl/750329 to your computer and use it in GitHub Desktop.
require 'rubygems'
$LOAD_PATH.unshift File.dirname(__FILE__) + '/lib'
require 'sinatra'
require 'erubis'
set :erubis, :pattern => '\{% %\}'
require 'rdiscount'
get '/about' do
"I'm running version " + Sinatra::VERSION
end
get '/' do
@title = "Hello Markdown!"
markdown :index, :layout_engine => :erubis
end
__END__
@@layout
<hr>
{%= yield -%}
<hr>
@@ index
{%= @title %}
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!
@wbzyl
Copy link
Author

wbzyl commented Dec 21, 2010

commit : 29e45e

„This will render an erb file with a haml layout:

get('/') { erb :index, :layout_engine => :haml }

Whereas this will cause all markdown templates to be rendered with an erb layout.”

Although the index template is rendered with erubis, the {%= @title %} chunk
is not rendered with erubis. See the gist example.

I want to render the index template with markdown and with erubis:

erubis(markdown(:index))

and expect the following output:

<p>Hello Markdown!</p>

Are there any chances to make this work?

I use this trick in my blogs to set titles and to generate
links to static assets (images, css, javascript).
My blogs are powered by the Sinatra version <1.0 and
my sinatra-rdiscount & sinatra-static-assets extensions.

See

http://sinatra.inf.ug.edu.pl/rails4/
http://sinatra.inf.ug.edu.pl/seminarium/
http://sinatra.inf.ug.edu.pl/sp/

Regards

--Włodek Bzyl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment