slim icon indicating copy to clipboard operation
slim copied to clipboard

Can't capture blocks or use instance variables

Open activestylus opened this issue 3 years ago • 0 comments

I'm rolling my own static site generator with Slim and am struggling to pass content to the layout from the page using instance variables or content_for.

Lifted some code from Sinatra but no luck so far

def render_slim
  data = {"title" => "Hello"}
  layout  = Slim::Template.new("layout.slim")
  page = Slim::Template.new("page.slim").render(Object.new, data)
  layout.render(Object.new, data) { content } 
end

# From Sinatra
def content_for(key, &block)
  content_blocks[key.to_sym] << block.call
  return ""
end

def content_for?(key)
  content_blocks[key.to_sym].any?
end

def yield_content(key, *args)
  content_blocks[key.to_sym].join
end

def content_blocks
  @content_blocks ||= Hash.new {|h,k| h[k] = [] }
end
/ page.slim
- @title = 'My Title'
h1=data['title']
= content_for :css do
  | body {color:red}

/ layout.slim
html
  head
    title= @title
    style
      == yield_content :css

Not throwing any errors, I simply end up with empty title and style tags in my layout.

What am I missing?

activestylus avatar May 04 '22 07:05 activestylus