chr*_*aly 10 ruby haml slim-lang
我试图在独立(不是rails)应用程序中做这样的事情:
layout.slim:
h1 Hello
.content
= yield
Run Code Online (Sandbox Code Playgroud)
show.slim:
= object.name
= object.description
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何指定布局和模板.苗条(或haml)可能吗?谢谢.
sto*_*ean 19
layout.slim文件如下所示:
h1 Hello
.content
== yield
Run Code Online (Sandbox Code Playgroud)
contents.slim文件如下所示:
= name
Run Code Online (Sandbox Code Playgroud)
这可以缩短,但为了便于说明,我将其分为各个步骤.
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 }
Run Code Online (Sandbox Code Playgroud)
这将输出:
<h1>Hello</h1><div class="content">test this layout</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3860 次 |
| 最近记录: |