Created
December 21, 2010 18:25
-
-
Save wbzyl/750329 to your computer and use it in GitHub Desktop.
Does not work as advertised: https://github.com/sinatra/sinatra/commit/29e45e9f4f6a2a12b873700f71c44e77083a9634
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 '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 %} |
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 '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! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
commit : 29e45e
„This will render an erb file with a haml layout:
Whereas this will cause all markdown templates to be rendered with an erb layout.”
Although the index template is rendered with erubis, the
{%= @title %}
chunkis not rendered with erubis. See the gist example.
I want to render the index template with markdown and with erubis:
and expect the following output:
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
Regards
--Włodek Bzyl