Sinatra render and passing rendering options for Redcarpet

1k views Asked by At

I’m using Sinatra, and Sinatra uses Tilt for template rendering.

By default Redcarpet has many rendering extensions. How can I use some of these extensions via Sinatra’s #render method?

I need to render markdown files with :gh_codeblock extension.

2

There are 2 answers

0
topek On

If I read the source code of sinatra, tilt and redcarpet right, you should be able to do something like this:

render('your_view', {:gh_codeblock => true}, {HASH_OF_YOUR_LOCAL_VARIABLES})

The second parameter to render is an options hash that is passed to the template engine. See:

def markdown(template, options={}, locals={})
  render :markdown, template, options, locals
end
0
matt On

In Sinatra, you don’t usually use the render method directly, rather you use the method corresponding to the appropriate template language, in this case markdown.

You should be able to pass any options you want to this method as a hash, and Sinatra (and Tilt) will pass them to the template engine. However, the latest released Tilt gem (1.3.3) doesn’t pass all the options for markdown through, only :filter_html and :smart, so this won’t work. This is fixed in the current Tilt head, it just hasn’t made its way to a released gem yet.

If you’re using Bundler, you could work around this by using Bundler’s Git support:

gem 'tilt', :git => 'git://github.com/rtomayko/tilt.git'

Alternatively you could download the latest version of Tilt and make sure its lib directory is on your apps load path, perhaps putting it into a vendor directory.