Search code examples
pythonflaskjinja2

app_template_filter with multiple arguments


How can I pass two arguments to app_template_filter (doc)? This works well if I use only one argument. But in this case, I need two.

@mod.app_template_filter('posts_page')
def posts(post_id, company_id):
    pass

{{ post.id, post.company.id | posts_page }}

Error:

TypeError: posts_page() takes exactly 2 arguments (1 given)

Solution

  • From the Jinja docs,

    Variables can be modified by filters. Filters are separated from the variable by a pipe symbol (|) and may have optional arguments in parentheses. Multiple filters can be chained. The output of one filter is applied to the next.

    Filters are designed to modify one variable at a time. You're looking for a context processor:

    Variables are not limited to values; a context processor can also make functions available to templates (since Python allows passing around functions)

    For example,

    @app.context_processor
    def add():
        def _add(int1, int2):
            return int(int1) + int(int2)
        return dict(add=_add)
    

    can be used in the template as

    {{ add(a, b) }}
    

    You can adopt this as your posts_page method:

    @app.context_processor
    def posts_page():
        def _posts_page(post_id, company_id):
            # ...
            return value
        return dict(posts_page=_posts_page)