我使用Haml (Haml/Sass 3.0.9 - Cassidy)独立生成静态HTML。我想要创建一个共享布局模板,所有其他模板都继承它。
Layout.haml
%html
%head
%title Test Template
%body
.ContentContent.haml
SOMEHOW INHERIT Layout.haml
SOMEHOW Change the title of the page "My Content".
%p This is my content生产:
Content.html
<html>
<head>
<title>My Content</title>
</head>
<body>
<div class="Content">
<p>This is my content</p>
</div>
</body>
</html>但这似乎不可能。我见过在Rails中使用Haml时使用呈现部分的用法,但在使用Haml独立时却找不到任何解决方案。
必须将布局代码放入所有模板中将是维护的噩梦;因此,我的问题是如何避免这样做?有标准的方法来解决这个问题吗?我错过了一些最基本的东西吗?
我发现了一个类似的问题:从Rails的HAMLoutside中呈现HAML部分
发布于 2010-06-03 11:00:15
我创造了一个原型来满足我的需要。我只需要将其创建为一个模块,并允许它接受布局模板和内容模板作为参数(以及一个数据集)。
require "haml"
layoutTemplate = File.read('layout.haml')
layoutEngine = Haml::Engine.new(layoutTemplate)
layoutScope = Object.new
output = layoutEngine.render(scope=layoutScope) { |x|
case x
when :title
scope.instance_variable_get("@haml_buffer").buffer << "My Title\n"
when :content
contentTemplate = File.read('page.haml')
contentEngine = Haml::Engine.new(contentTemplate)
contentOutput = contentEngine.render
scope.instance_variable_get("@haml_buffer").buffer << contentOutput
end
}
puts outputlayout.haml
%html
%head
%title
- yield :title
%body
.content
- yield :contentpage.haml
%h1 Testing
%p This is my test page.输出
<html>
<head>
<title>
My Title
</title>
</head>
<body>
<div class='content'>
<h1>Testing</h1>
<p>This is my test page.</p>
</div>
</body>
</html>发布于 2010-06-02 23:00:01
Haml构建时假定它将与提供部分和布局之类的Ruby框架一起使用。如果您想要一种简单的方法来呈现带有布局和部分的静态Haml代码,请查看StaticMatic。
发布于 2011-07-28 12:01:38
StaticMatic已经被它的创建者抛弃了,后者现在使用http://middlemanapp.com/。
https://stackoverflow.com/questions/2961718
复制相似问题