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

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
主站蜘蛛池模板: 个旧市| 罗平县| 东乌珠穆沁旗| 澄城县| 邻水| 如皋市| 博野县| 荔浦县| 天水市| 泽库县| 武威市| 广西| 大渡口区| 思南县| 广德县| 乌拉特中旗| 凉城县| 会理县| 定西市| 监利县| 通江县| 巴青县| 依安县| 岳池县| 南华县| 房山区| 三原县| 黔东| 横山县| 扎囊县| 通道| 满洲里市| 祁连县| 南投县| 长乐市| 四会市| 波密县| 宁夏| 同仁县| 大姚县| 平果县|