Подтвердить что ты не робот

Указание макета и шаблона в автономном (не рельсовом) рубиновом приложении с использованием slim или haml

Я пытаюсь сделать что-то подобное в автономном (не рельсовом) приложении:

layout.slim:

h1 Hello
.content
  = yield

show.slim:

= object.name
= object.description

Я не могу понять, как указать макет и шаблон. Возможно ли это с тонким (или haml)? Спасибо.

4b9b3361

Ответ 1

Файл layout.slim выглядит так:

h1 Hello
.content
  == yield

Файл contents.slim выглядит так:

= name

Это может быть сокращено, но для объяснения я разделяю отдельные шаги.

require 'slim'

# Simple class to represent an environment
class Env
  attr_accessor :name
end

# Intialize it
env = Env.new
# Set the variable we reference in contents.slim
env.name = "test this layout"

# Read the layout file in as a string
layout = File.open("layout.slim", "rb").read

# Read the contents file in as a string
contents = File.open("contents.slim", "rb").read

# Create new template object with the layout
l = Slim::Template.new { layout }

# Render the contents passing in the environment: env
# so that it can resolve: = name
c = Slim::Template.new { contents }.render(env)

# Render the layout passing it the rendered contents
# as the block. This is what yield in layout.slim will get
puts l.render{ c }

Это выведет:

<h1>Hello</h1><div class="content">test this layout</div>