2016-08-30 10 views
1

マイページには、識別子と実際の内容を除き、常に同じマークアップを持つ多くのブートストラップモーダルがあります。JinjaマクロへのHTMLの受け渡し

私は1つの単純な呼び出し、例えばモーダルのための全体のHTMLマークアップを吐き出す神社マクロを構築したいこれらのモーダルのコードの巨大なとrepetetive量を最小限に抑えるために:

{# macros/modal_template.jinja2 #} 

{% macro print_modal(id, title, body_content) %} 
    <div class="modal fade" id="{{ id }}" tabindex="-1"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal"><span>&times;</span></button> 
      <h4 class="modal-title">{{ title }}</h4> 
     </div> 
     <div class="modal-body"> 
      {{ body_content }} 
     </div> 
     <div class="modal-footer"> 
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
      <button type="button" class="btn btn-primary">Save changes</button> 
     </div> 
     </div> 
    </div> 
    </div> 
{% endmacro %} 


{# my_page.jinja2 #} 

{% from "macros/modal_template.jinja2" import print_modal %} 
<html> 
<body> 
    {{ print_modal("description-modal", "Description", "Lorem Ipsum") }} 
</body> 
</html> 

これまでのところ、それはです画像はbody_contentは普通の文字列ではありませんが、複雑なHTMLフォームや、テキストスタイルのHTMLを持つ本当に長いテキストです。私はこの問題を解決するために苦労しています。

これまでに私が思いついた唯一の解決策は、コンテンツを文字列として渡して{{ body_content|safe }}で印刷することでしたが、複雑なマークアップを文字列に入れるのは醜く不快です。

あなたはいいアイディアを持っていますか?

答えて

3

少し遅れ答え - 私はちょうど同じことをグーグルであなたの質問に出くわしたが、私はそれを考え出した:

あなたは何ができるので、同様jinjas呼び出し/呼び出し側()の機能を利用することである。..

{% macro print_modal(id, title) %} 
    <div class="modal fade" id="{{ id }}" tabindex="-1"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
     <div class="modal-header"> 
      <button type="button" class="close" data-dismiss="modal"><span>&times;</span></button> 
      <h4 class="modal-title">{{ title }}</h4> 
     </div> 
     <div class="modal-body"> 
      {{ caller() }} <---- {# Look at me! #} 
     </div> 
     <div class="modal-footer"> 
      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
      <button type="button" class="btn btn-primary">Save changes</button> 
     </div> 
     </div> 
    </div> 
    </div> 
{% endmacro %} 

としたときにマクロ

{% call print_modal(someId, someTitle) %} 
    {# whatever you put in here will be placed into the {{caller()}} line #} 
    {# for example #} 
    <h1>HELLO WORLD</h1> {# Will format an h1 into the caller block #} 
{% endcall %} 

EDIT利用:スペル/スタイル

関連する問題