简体   繁体   English

外部Haml变量

[英]External Haml Variable

I created a layout with a variable inside. 我创建了一个内部带有变量的布局。

layout.haml: layout.haml:

- title = "Example"
%title #{title}

It works perfect and gave me this: 它工作完美,给了我这个:

<title>Example</title>

But, if I put this variable in a partial, it doesn't work 但是,如果我将此变量放在部分变量中,它将不起作用

_vars.haml: _vars.haml:

- title = "Example"

layout.haml: layout.haml:

=partial "vars"
%title #{title}

How can I define all the variables on an external document and make it work? 如何定义外部文档上的所有变量并使之工作?

Thanks for the help 谢谢您的帮助

You are probably looking for content for: 您可能正在寻找以下内容:

layout.html.haml: layout.html.haml:

%title= yield(:title)

_my_partial.html.haml: _my_partial.html.haml:

- content_for(:title) do
  Example

Maybe you could put your shared code in helper? 也许您可以将共享代码放入帮助程序中?

# application_helper.rb
def title
  @title ||= 'Example'
end

After that title helper could be used either in primary view or in partial. 之后,可以在主视图或局部视图中使用title助手。 Notice that calculation of variable will be performed only once due to ||= . 注意,由于||= ,变量的计算将仅执行一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM