Last active
May 6, 2021 15:39
-
-
Save Yorkshireman/9d1a79d7c3281059e03dc6ed5e74cea4 to your computer and use it in GitHub Desktop.
slim partials in Sinatra
This file contains hidden or 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
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! |
This file contains hidden or 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 '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 |
This file contains hidden or 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
== partial :_hello_world, locals: { name: 'Andrew' } |
This file contains hidden or 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
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