2010-05-19 3 views
3

Jinja2 템플리트에서 목차와 미주를 작성하려고합니다. 어떻게 이러한 작업을 수행 할 수 있습니까? some other file with content.jnj이 같은 내용이별도의 장소에 내용을 넣는 Jinja2 매크로 작성

{% block toc %} 
{# ... the ToC goes here ... #} 
{% endblock %} 

{% include "some other file with content.jnj" %} 

{% block endnotes %} 
{# ... the endnotes go here ... #} 
{% endblock %} 

는 :

내가 말할
{% section "One" %} 
Title information for Section One (may be quite long); goes in Table of Contents 
... 
Content of section One 

{% section "Two" %} 
Title information of Section Two (also may be quite long) 

<a href="#" id="en1">EndNote 1</a> 
<script type="text/javsacript">...(may be reasonably long) 
</script> {# ... Everything up to here is included in the EndNote #} 

나는 "아주/합리적으로 긴 될 수있다"는 다음과 같이

예를 들어, 나는 템플릿을 갖고 싶어 매크로 또는 전역 함수에 대한 인수로 합리적으로 따옴표로 묶을 수 없다는 것을 의미합니다.

Jinja2의 프레임 워크 내에서이를 수용 할 수있는 패턴이 있는지 궁금합니다.

내 초기 생각은 하나의 섹션과 최종 메모 블록을 가질 수 있도록, 확장을 만드는 것입니다 마치 너무 :

{% section "One" %} 
Title information goes here. 
{% endsection %} 

{% endnote "one" %} 
<a href="#">...</a> 
<script> ... </script> 
{% endendnote %} 

그런 다음 전역 함수를합니다 (Jinja2 환경에서 그 패스) :

{{ table_of_contents() }} 

{% include ... %} 

{{ endnotes() }} 

그러나 이것은 미주를 위해 작동하지만, 내용의 목차를 위해 두 번째 패스가 필요합니다.

읽어 주셔서 감사합니다. 나는 당신의 생각과 의견을 많이 지켜야 할 것입니다. 각 섹션에 대한 일관된 구조를 정의하는 올바른 경로 (즉 제목, 본문, 미주)을 아래로 향하고있다처럼

브라이언

답변

2

보인다. Jinja 블록 및/또는 사용자 정의 확장이 아닌 일반 Python 데이터 구조에이 정보를 저장하는 것이 허용됩니까? 아래 예.

content.jnj에 :

{% set sections = [] %} 
{% do sections.append({ 
'title': 'Section One title', 
'body': 
""" 
Section one main body text... 
""", 
'endnotes': 
""" 
<a href='#'>...</a> 
<script> ... </script> 
""", 
}) 
%} 
{# append more sections #} 

template.jnj의 : content.jnj는 Jinja2 do 확장을 필요로

{% from 'content.jnj' import sections as sections %} 
{# table of contents #} 
{% for section in sections %} 
    {{ loop.index }}. {{ section['title'] }} 
{% endfor %} 
{# body #} 
{% for section in sections %} 
    {{ section['title'] }} 
    {{ section['body'] }} 
{% endfor %} 
{# endnotes #} 
{% for section in sections %} 
    {{ loop.index }}. {{ section['endnotes'] }} 
{% endfor %} 

하는 것으로 (예 : env = jinja2.Environment(extensions=['jinja2.ext.do']).)를 활성화한다 그것은 잔인한있을 수 있습니다

당신의 다른 옵션은 마크 업 언어로 컨텐츠를 저장하는 것이지만, reStructuredText와 같은 프리젠 테이션 레이어를 만들고 Sphinx 테마 (Jinja2가 기본 템플릿 형식 임)로 프리젠 테이션 레이어를 디자인하십시오.

관련 문제