官术网_书友最值得收藏!

  • Flask Framework Cookbook
  • Shalabh Aggarwal
  • 267字
  • 2021-08-05 17:17:22

Creating a custom Jinja2 filter

After looking at the previous recipe, experienced developers might think that it was stupid to use a context processor to create a descriptive product name. We can simply write a filter to get the same result; this will make things much cleaner. A filter can be written to display the descriptive name of the product as shown here:

@product_blueprint.template_filter('full_name')
def full_name_filter(product):
    return '{0} / {1}'.format(product['category'], product['name'])

This can be used as follows:

{{ product|full_name }}

The preceding code will yield a similar result as it did in the previous recipe.

How to do it…

To take things to a higher level, let's create a filter to format the currency based on the current local language:

import ccy
from flask import request

@app.template_filter('format_currency')
def format_currency_filter(amount):
    currency_code = ccy.countryccy(request.accept_languages.best[-2:])
    return '{0} {1}'.format(currency_code, amount)

Note

The request.accept_languages list might now work in cases where a request does not have the ACCEPT-LANGUAGES header.

The preceding snippet will require the installation of a new package, ccy:

$ pip install ccy

The filter created here takes the language that best matches the current browser locale (which, in my case, is en-US), takes the last two characters from the locale string, and then gets the currency as per the ISO country code that is represented by the last two characters.

How it works…

The filter can be used in our template for the product as shown:

<h3>{{ product['price']|format_currency }}</h3>

It will yield the result shown in the following screenshot:

See also

  • Check out the Block composition and layout inheritance recipe to understand the context of this recipe
主站蜘蛛池模板: 浦县| 来安县| 黔江区| 包头市| 马公市| 南涧| 庆阳市| 罗定市| 巢湖市| 海安县| 观塘区| 岑巩县| 固原市| 涟源市| 泗阳县| 甘孜| 广安市| 三门峡市| 甘肃省| 桃江县| 河津市| 灵璧县| 韶关市| 阿克苏市| 都昌县| 竹山县| 措勤县| 巩留县| 平利县| 扎鲁特旗| 扶绥县| 奎屯市| 喀喇沁旗| 封丘县| 饶平县| 左贡县| 海原县| 图木舒克市| 广元市| 阿图什市| 顺平县|