2010-06-02 3 views
5

정적 HTML을 생성하기 위해 Haml (Haml/Sass 3.0.9 - Classy Cassidy) 독립 실행 형을 사용하고 있습니다. 다른 모든 템플릿이 상속하는 공유 레이아웃 템플릿을 만들고 싶습니다.Haml Inherit Templates

Layout.haml

%html 
    %head 
    %title Test Template 
    %body 
    .Content 

Content.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> 

하지만 이는 가능하지 않습니다. Haml을 Rails와 함께 사용할 때 부분 렌더링을 사용하는 것을 보았지만 Haml을 단독으로 사용할 때 어떤 해결책도 찾을 수 없습니다.

모든 템플릿에 레이아웃 코드를 삽입해야하는 것은 유지 관리의 악몽입니다. 그래서 내 질문은 어떻게 이런 일을 피하지? 이 문제를 해결하기위한 표준 방법이 있습니까? 근본적으로 뭔가를 놓친 적이 있습니까? Rendering HAML partials from within HAMLoutside of Rails

답변

4

필자가 필요로하는 프로토 타입을 만들었습니다. 이 코드를 모듈로 만들고 레이아웃 템플릿과 컨텐트 템플릿을 인수로 받아 들일 수 있어야합니다 (데이터 셋 포함).

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 output 

layout.haml

%html 
    %head 
    %title 
     - yield :title 
    %body 
    .content 
     - yield :content 

page.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> 
2

HAML이이 부분 지문 및 레이아웃 등을 제공합니다 일부 루비 프레임 워크와 함께 사용됩니다 있다는 가정하에 구축 :

나는 비슷한 질문을 발견했다. 레이아웃과 부분으로 정적 인 Haml 코드를 렌더링하는 간단한 방법을 원하면 StaticMatic을 확인하십시오.

+0

+1 StaticMatic을 (를) 보았습니다. 그것은 레이아웃 템플릿에 대해 원하는 것을 정확히 제공하지만 다른 영역에서는 문제를 제기합니다. – kjfletch