2013-01-10 3 views
8

하위 페이지의 변수를 템플릿에 전달하려고합니다. 이것은 내 파이썬 코드 :Jinja2 부모 변수를 전달하는 방법이 있습니까?

if self.request.url.find("&try") == 1: 
     isTrying = False 
    else: 
     isTrying = True 

    page_values = { 
     "trying": isTrying 
    } 

    page = jinja_environment.get_template("p/index.html") 
    self.response.out.write(page.render(page_values)) 

템플릿 :

<html> 
    <head> 
    <link type="text/css" rel="stylesheet" href="/css/template.css"></link> 
    <title>{{ title }} | SST QA</title> 

    <script src="/js/jquery.min.js"></script> 

    {% block head %}{% endblock head %} 
    </head> 
    <body> 
    {% if not trying %} 
    <script type="text/javascript"> 
    // Redirects user to maintainence page 
    window.location.href = "construct" 
    </script> 
    {% endif %} 

    {% block content %}{% endblock content %} 
    </body> 
</html> 

와 아이 :

{% extends "/templates/template.html" %} 
{% set title = "Welcome" %} 
{% block head %} 
{% endblock head %} 
{% block content %} 
{% endblock content %} 
문제가

, 나는 부모로 "노력"변수를 전달하려면 , 이것을 할 수있는 방법이 있습니까?

미리 감사드립니다.

답변

2

문제점을 이해하지 못합니다. 컨텍스트에 변수를 전달하면 (시도와 마찬가지로) 이러한 변수는 하위 및 상위 변수에서 사용할 수 있습니다. 부모에 제목을 전달하려면, 당신은 슈퍼 때로는 함께, 상속을 사용해야합니다 : Overriding app engine template block inside an if

+0

네 , 미안 해요. 문제를 알아 냈습니다. 대답은 간단합니다. 그것은 나에게 많은 문제를 준다. –

+2

"대답은 간단합니다 -하지 마세요." 변수를 사용하는 대신 보통 중첩 된 블록을 상위 블록에 추가 한 다음 하위 블록에 채 웁니다. –

14

Jinja2 팁과 트릭 페이지의 예는 http://jinja.pocoo.org/docs/templates/#base-template이 완벽하게 설명 :

http://jinja.pocoo.org/docs/templates/#super-blocks 또한이 질문을 참조하십시오. 당신이 기본 템플릿

**base.html** 
<html> 
    <head> 
     <title> MegaCorp -{% block title %}{% endblock %}</title> 
    </head> 
    <body> 
     <div id="content">{% block content %}{% endblock %}</div> 
    </body> 
</html> 

과 파이썬 함수는 render_template ("child.html을")를 호출 어떤 아이 템플릿

**child.html** 
{% extends "base.html" %} 
{% block title %} Home page {% endblock %} 
{% block content %} 
... stuff here 
{% endblock %} 

이있는 경우 기본적으로 HTML 페이지를 반환합니다

**Rendered Page** 
<html> 
    <head> 
     <title> MegaCorp - Home </title> 
    </head> 
    <body> 
     <div id="content"> 
      stuff here... 
     </div> 
    </body> 
</html> 
+3

이 질문은 질문의 구성에 따라 정답으로 표시되어야합니다. – David

관련 문제