Skip to content

Instantly share code, notes, and snippets.

@emilebosch
Created October 21, 2014 23:21
Show Gist options
  • Save emilebosch/9e5c25833b8f92dfd4db to your computer and use it in GitHub Desktop.
Save emilebosch/9e5c25833b8f92dfd4db to your computer and use it in GitHub Desktop.
Make it possibe to have jade style mixins in slim http://jade-lang.com/reference/mixins/
def mixin(name, &block)
@mixins ||= {}
@mixins[name] = block
method = %{
def #{name}(*args, &block)
call_mixin(:#{name}, *args, &block)
end
}
eval(method)
end
def call_mixin(name, *args, &block)
x = args || []
x.unshift block
capture *x, &@mixins[name]
end
- mixin :list do |block, title|
h1=title
ul==capture(&block)
= list "Ferrets" do
li Item Tobi
li Item Loki
@aaronransley
Copy link

@leouz Keep in mind that your solution does not account for arguments being passed into the block

@allenwu1973
Copy link

or use lambda

- hello = ->(*names) do
  - capture do
    .greet
      p= "Hello #{name}"

div.greetings
  = hello["John"]
  = hello["Steve"]

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