2016-11-27 1 views
3

jinja2를 템플릿 엔진으로 사용하여 특정 파일을 생성하는 스크립트를 작성하고 있습니다. 마지막 줄을 제외하고 내가 예상 한 파일을 생성합니다. 템플릿에서 마지막 빈 줄을 지정했지만 파일을 만들 때 해당 줄이 없습니다.jinja2가 마지막 줄을 무시하고 있습니까?

템플릿은 다음과 같습니다 : 총 그래서

# -*- coding: utf-8 -*- 

from openerp import fields, models, api 


class {{ class_name }}(models.{{ model_type }}): 
    """{{ class_docstring }}""" 

    _{{ def_type }} = '{{ model }}' 
# Here is actually empty line. Note comment does not exist on template. It is just empty line. 

이 템플릿에 정의 된 10 선이있다. 그러나이 템플리트를 사용하여 작성된 파일에는 9 행만 있습니다 (마지막 행은 작성되지 않습니다).

예상되는 동작입니까, 아니면 예상대로 마지막 줄을 만들어야합니까?

다음

데이터와 방법을 렌더링 처리 :

from jinja2 import Environment, FileSystemLoader 

PATH = os.path.dirname(os.path.abspath(__file__)) 
TEMPLATE_ENVIRONMENT = Environment(
    autoescape=True, 
    loader=FileSystemLoader(os.path.join(PATH, 'templates')), 
    trim_blocks=False) 
... 
... 
    @staticmethod 
    def render_template(t, context): 
     # For now it only supports standard templates. 
     template_filename = TEMPLATE_FILES_MAPPING[t] 
     return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(
      context) 

답변

0

keep_trailing_newline 옵션은 당신이 찾고있는 무엇을 할 수있다 : 기본적으로

는 Jinja2는 줄 바꿈을 후행 제거합니다. 단일 줄 바꿈을 유지하려면 Jinja를 keep_trailing_newline으로 구성하십시오.

당신은 환경에 추가 할 수 있습니다

:

TEMPLATE_ENVIRONMENT = Environment(
    ... 
    keep_trailing_newline=True) 
관련 문제