推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
hao1032
V2EX  ›  Python

jinjia2 有这样的函数吗?{{ value|xxx("good", "bad") }}

  •  
  •   hao1032 · May 24, 2014 · 5027 views
    This topic created in 4381 days ago, the information mentioned may be changed or developed.
    {{ value|xxx("good", "bad") }}
    如果value为true,显示good,反之显示bad
    8 replies    2014-07-16 17:00:55 +08:00
    casparchen
        1
    casparchen  
       May 24, 2014
    {% if True %}
    good
    {% else %}
    bad
    {% endif %}
    hao1032
        2
    hao1032  
    OP
       May 24, 2014
    @casparchen
    <input type="radio" name="types" id="video_url" value="video_url" checked>
    我是要checked是否显示的,并且共有4个radio呢,用if写太难看了。
    zhy0216
        3
    zhy0216  
       May 24, 2014
    @hao1032 可以自定义哈
    casparchen
        4
    casparchen  
       May 24, 2014
    {% set str = "" %}

    {% if True %}
    {% set str = "checked" %}
    {% endif %}
    然后在每个radio输出str不行么?
    casparchen
        5
    casparchen  
       May 24, 2014   ❤️ 1
    好吧,查了一下文档,有这个:
    {{ 'good' if 1==1 else 'bad' }}

    The general syntax is <do something> if <something is true> else <do something else>.
    hao1032
        6
    hao1032  
    OP
       May 24, 2014   ❤️ 1
    @casparchen 这个可以用。厉害
    请问你是怎么查到文档啊,我怎么没有找到呢?
    casparchen
        7
    casparchen  
       May 25, 2014
    zjnjxufe
        8
    zjnjxufe  
       Jul 16, 2014
    flask里注册一个过滤器就可以这么用了,有没有现成的函数可以直接这么用就不清楚了,可以去看一下jinjia2的内置函数。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   951 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 22:18 · PVG 06:18 · LAX 15:18 · JFK 18:18
    ♥ Do have faith in what you're doing.