Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Yorkshireman
Last active May 6, 2021 15:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yorkshireman/9d1a79d7c3281059e03dc6ed5e74cea4 to your computer and use it in GitHub Desktop.
Save Yorkshireman/9d1a79d7c3281059e03dc6ed5e74cea4 to your computer and use it in GitHub Desktop.
slim partials in Sinatra
h1 Hello, World! Hello #{name}!
/ I initially had only `.render({}, locals)`, which meant that the partials didn't
/ have access to any helper methods contained inside `OtherHelperMethods` (but `home.slim` did).
/ Passing `self` into `.render`, as the first argument, fixes that (if you're curious
/ about that, look up the `Tilt::Template #render` documentation.
/ With this PartialsHelper, passing locals is optional, as is specifying a different
/ path to the partial (relative to `settings.views`).
/ Hope you get as much use out of it as I do!
require 'slim'
require 'slim/include'
require 'partials_helper'
require 'other_helper_methods'
class App < Sinatra::Base
helpers do
include PartialsHelper
include OtherHelperMethods
end
get '/' do
slim :home
end
end
== partial :_hello_world, locals: { name: 'Andrew' }
module PartialsHelper
def partial(name, path: '/partials', locals: {})
Slim::Template.new("#{settings.views}#{path}/#{name}.slim").render(self, locals)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment