可重复使用的超薄参数?
Reusable slim with parameters?
我有一个日期部分如下,目前是硬编码的:
#postdate
.grid
.grid__cell.day 15th
.grid__cell.month Jan
.grid__cell.year 2015
但是,有没有办法通过使日期动态化来使它可重用,以便我可以从另一个 slim 文件传递日期?
我不确定该怎么做,因为您只需添加 == slim :'components/_date'
即可渲染部分内容。有没有更好的方法来完成这类事情?
我将 Ruby 与 sass 和 sinatra 一起使用。
谢谢!
...is there a way to make this reusable by making the date dynamic so
that I can pass in the date...
在 sinatra 路由中,您可以设置变量,这些变量可在您的 slim 模板中访问:
routes.rb:
require 'sinatra'
require 'slim'
get '/' do
@day = 170000 #****HERE****
slim :index
end
views/index.苗条:
h2 This is views/index.slim:
div The day is: #{@day} #****HERE****
ul.fruit
li Apples
li Oranges
== slim :'components/_date'
views/components/_date.苗条:
#postdate
.grid
.grid__cell.day =@day #****HERE*****
.grid__cell.month Jan
.grid__cell.year 2015
我不太明白你的意思:
pass in the date from another slim file
你能举例说明你想做什么吗?
回复评论:
根据 Sinatra 常见问题,How do I render partials?,如果你想渲染一个部分并为部分指定局部变量,你需要安装一个 gem:
$ gem install sinatra-partial
这是一个示例应用程序:
~/sinatra_projects$ tree slim_app/
slim_app/
├── models
├── public
│ ├── cool_stuff.html
│ └── imgs
├── routes.rb
└── views
├── components
│ └── _date.slim
├── index.slim
└── layout.slim
routes.rb:
require 'sinatra'
require 'slim'
#***NEW STUFF:
require 'sinatra/partial'
set :partial_template_engine, :slim
enable :partial_underscores
get '/' do
slim :index
end
views/components/index.苗条:
h2 This is views/index.slim:
ul.fruit
li Apples
li Oranges
== partial\
:'components/date',
locals: {day: 1, month: 'Jan', year: 2015}
== partial\
:'components/date',
locals: {day: 25, month: 'Dec', year: 2014}
如果你使用各种模板,erb、haml、slim等,你可以在调用partial()时设置类型:
partial(:"components/date", :template_engine => :slim, :locals => {...})
views/components/_date.苗条:
#postdate
.grid
.grid__cell.day =day
.grid__cell.month =month
.grid__cell.year =year
views/layout.苗条:
doctype html
html
head
meta charset="utf-8"
title Test
body
h1 This is the Layout. Find me in /views/app_layout.slim
== yield
所以,我假设您想做这样的事情:
routes.rb:
get '/' do
@dates = [
{day: 1, month: 'Jan', year: 2015},
{day: 2, month: 'Feb', year: 2015},
{day: 3, month: 'Mar', year: 2015},
{day: 4, month: 'Apr', year: 2015},
]
slim :index
end
views/index.苗条:
h2 This is views/index.slim:
ul.fruit
li Apples
li Oranges
- @dates.each do |date|
== partial\
:'components/date',
locals: {day: date[:day], month: date[:month], year: date[:year]}
views/components/_date.苗条:
#postdate
.grid
.grid__cell.day =day
.grid__cell.month =month
.grid__cell.year =year
...产生:
这是布局。在 /views/app_layout.slim
中找到我
这是views/index.slim:
- 苹果
- 橘子
1
一月
2015
2
二月
2015
3
三月
2015
4
四月
2015
我遇到了类似的问题,并设法编写了一个杀手级的分音助手,它为您提供了类似于 Rails 的分音功能。我真的很满意!
#partials_helper.rb
module PartialsHelper
def partial(name, path: '/partials', locals: {})
Slim::Template.new("#{settings.views}#{path}/#{name}.slim").render(self, locals)
end
end
-
#app.rb
require 'slim'
require 'slim/include'
require 'partials_helper'
require 'other_helper_methods'
class App < Sinatra::Base
helpers do
include PartialsHelper
include OtherHelperMethods
end
get '/' do
slim :home
end
end
-
#views/home.slim
== partial :_hello_world, locals: { name: 'Andrew' }
-
#views/partials/_hello_world.slim
h1 Hello, World! Hello #{name}!
我最初只有 .render({}, locals)
,这意味着部分无法访问 OtherHelperMethods
中包含的任何辅助方法(但 home.slim
可以)。将 self
作为第一个参数传递给 .render
,可以解决这个问题(如果您对此感到好奇,请查阅 Tilt::Template #render
文档。
使用此 PartialsHelper,传递局部变量是可选的,指定部分的不同路径也是如此(相对于 settings.views
)。
希望你能像我一样充分利用它!
我有一个日期部分如下,目前是硬编码的:
#postdate
.grid
.grid__cell.day 15th
.grid__cell.month Jan
.grid__cell.year 2015
但是,有没有办法通过使日期动态化来使它可重用,以便我可以从另一个 slim 文件传递日期?
我不确定该怎么做,因为您只需添加 == slim :'components/_date'
即可渲染部分内容。有没有更好的方法来完成这类事情?
我将 Ruby 与 sass 和 sinatra 一起使用。
谢谢!
...is there a way to make this reusable by making the date dynamic so that I can pass in the date...
在 sinatra 路由中,您可以设置变量,这些变量可在您的 slim 模板中访问:
routes.rb:
require 'sinatra'
require 'slim'
get '/' do
@day = 170000 #****HERE****
slim :index
end
views/index.苗条:
h2 This is views/index.slim:
div The day is: #{@day} #****HERE****
ul.fruit
li Apples
li Oranges
== slim :'components/_date'
views/components/_date.苗条:
#postdate
.grid
.grid__cell.day =@day #****HERE*****
.grid__cell.month Jan
.grid__cell.year 2015
我不太明白你的意思:
pass in the date from another slim file
你能举例说明你想做什么吗?
回复评论:
根据 Sinatra 常见问题,How do I render partials?,如果你想渲染一个部分并为部分指定局部变量,你需要安装一个 gem:
$ gem install sinatra-partial
这是一个示例应用程序:
~/sinatra_projects$ tree slim_app/
slim_app/
├── models
├── public
│ ├── cool_stuff.html
│ └── imgs
├── routes.rb
└── views
├── components
│ └── _date.slim
├── index.slim
└── layout.slim
routes.rb:
require 'sinatra'
require 'slim'
#***NEW STUFF:
require 'sinatra/partial'
set :partial_template_engine, :slim
enable :partial_underscores
get '/' do
slim :index
end
views/components/index.苗条:
h2 This is views/index.slim:
ul.fruit
li Apples
li Oranges
== partial\
:'components/date',
locals: {day: 1, month: 'Jan', year: 2015}
== partial\
:'components/date',
locals: {day: 25, month: 'Dec', year: 2014}
如果你使用各种模板,erb、haml、slim等,你可以在调用partial()时设置类型:
partial(:"components/date", :template_engine => :slim, :locals => {...})
views/components/_date.苗条:
#postdate
.grid
.grid__cell.day =day
.grid__cell.month =month
.grid__cell.year =year
views/layout.苗条:
doctype html
html
head
meta charset="utf-8"
title Test
body
h1 This is the Layout. Find me in /views/app_layout.slim
== yield
所以,我假设您想做这样的事情:
routes.rb:
get '/' do
@dates = [
{day: 1, month: 'Jan', year: 2015},
{day: 2, month: 'Feb', year: 2015},
{day: 3, month: 'Mar', year: 2015},
{day: 4, month: 'Apr', year: 2015},
]
slim :index
end
views/index.苗条:
h2 This is views/index.slim:
ul.fruit
li Apples
li Oranges
- @dates.each do |date|
== partial\
:'components/date',
locals: {day: date[:day], month: date[:month], year: date[:year]}
views/components/_date.苗条:
#postdate
.grid
.grid__cell.day =day
.grid__cell.month =month
.grid__cell.year =year
...产生:
这是布局。在 /views/app_layout.slim
中找到我这是views/index.slim:
- 苹果
- 橘子
1
一月
2015
2
二月
2015
3
三月
2015
4
四月
2015
我遇到了类似的问题,并设法编写了一个杀手级的分音助手,它为您提供了类似于 Rails 的分音功能。我真的很满意!
#partials_helper.rb
module PartialsHelper
def partial(name, path: '/partials', locals: {})
Slim::Template.new("#{settings.views}#{path}/#{name}.slim").render(self, locals)
end
end
-
#app.rb
require 'slim'
require 'slim/include'
require 'partials_helper'
require 'other_helper_methods'
class App < Sinatra::Base
helpers do
include PartialsHelper
include OtherHelperMethods
end
get '/' do
slim :home
end
end
-
#views/home.slim
== partial :_hello_world, locals: { name: 'Andrew' }
-
#views/partials/_hello_world.slim
h1 Hello, World! Hello #{name}!
我最初只有 .render({}, locals)
,这意味着部分无法访问 OtherHelperMethods
中包含的任何辅助方法(但 home.slim
可以)。将 self
作为第一个参数传递给 .render
,可以解决这个问题(如果您对此感到好奇,请查阅 Tilt::Template #render
文档。
使用此 PartialsHelper,传递局部变量是可选的,指定部分的不同路径也是如此(相对于 settings.views
)。
希望你能像我一样充分利用它!