加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在独立(不是rails)的ruby应用程序中使用slim或haml来指定布局和

发布时间:2020-12-16 22:13:42 所属栏目:百科 来源:网络整理
导读:我想在独立(不是rails)的应用程序中做这样的事情: layout.slim: h1 Hello.content = yield show.slim: = object.name= object.description 我不知道如何指定布局和模板.这是可能与苗条(或哈姆尔)吗?谢谢. 解决方法 layout.slim文件如下所示: h1 Hello.c
我想在独立(不是rails)的应用程序中做这样的事情:

layout.slim:

h1 Hello
.content
  = yield

show.slim:

= object.name
= object.description

我不知道如何指定布局和模板.这是可能与苗条(或哈姆尔)吗?谢谢.

解决方法

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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读