Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use macros in a included file

Tags:

flask

jinja2

view.jinja

{% extends "layout/defaultlayout.jinja" %}
{% include('details.jinja') %}

defaultlayout.jinja

{% import 'elements/macros.jinja' as html %}

But i am not able to use the macro html in details.jinja without reincluding it

like image 491
aWebDeveloper Avatar asked Sep 25 '13 10:09

aWebDeveloper


1 Answers

Daniel's answer didn't help me. I had to import the following way

{% from "post_entity.html" import show_post with context %}

Here post_entity.html was file containing macro with show_post method
And then used following way:

{{ show_post(post) }}

Here post is a dictionary sent to template from flask render_template.
And the macro file file looked something like this:
post_entity.html

{% macro show_post(post) %}
    {{ post.photo_url }}
    {{ post.caption }}
{% endmacro %}
like image 71
Nabin Avatar answered Oct 17 '22 22:10

Nabin