11
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Jinja2でincludeしたファイルに変数を渡す方法

Last updated at Posted at 2019-10-02

はじめに

ちょっとした社内・部内向けのツールを作るのに、Python + bottle + jinja2 + SQLiteを使ってる。jinja2でincludeしたファイルに変数、パラメータを渡す方法がわからなかったのであれこれ調べたのでメモ。

やりかた

sample.py
{% with 変数名 = 渡す変数名 %}
{% include 'インクルードするファイル' %}
{% endwith %}

というように、{% with %} ~ {% endwith %} で囲んで、withのパラメータに渡したい変数を書く。

template.html
{% with name = name %} 
{% include 'include.html' %}
{% endwith %}
include.html
名前:{{ name }}

複数の変数を渡す場合

template.html
{% with name = name, address = address %} 
{% include 'include.html' %}
{% endwith %}
11
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?