V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2014-05-24 23:00:22 +08:00 · 4077 次点击
    这是一个创建于 3622 天前的主题,其中的信息可能已经有所发展或是发生改变。
    {{ value|xxx("good", "bad") }}
    如果value为true,显示good,反之显示bad
    8 条回复    2014-07-16 17:00:55 +08:00
    casparchen
        1
    casparchen  
       2014-05-24 23:09:36 +08:00
    {% if True %}
    good
    {% else %}
    bad
    {% endif %}
    hao1032
        2
    hao1032  
    OP
       2014-05-24 23:15:21 +08:00
    @casparchen
    <input type="radio" name="types" id="video_url" value="video_url" checked>
    我是要checked是否显示的,并且共有4个radio呢,用if写太难看了。
    zhy0216
        3
    zhy0216  
       2014-05-24 23:20:24 +08:00
    @hao1032 可以自定义哈
    casparchen
        4
    casparchen  
       2014-05-24 23:23:01 +08:00
    {% set str = "" %}

    {% if True %}
    {% set str = "checked" %}
    {% endif %}
    然后在每个radio输出str不行么?
    casparchen
        5
    casparchen  
       2014-05-24 23:26:00 +08:00   ❤️ 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
       2014-05-24 23:47:38 +08:00   ❤️ 1
    @casparchen 这个可以用。厉害
    请问你是怎么查到文档啊,我怎么没有找到呢?
    casparchen
        7
    casparchen  
       2014-05-25 00:50:29 +08:00
    zjnjxufe
        8
    zjnjxufe  
       2014-07-16 17:00:55 +08:00
    flask里注册一个过滤器就可以这么用了,有没有现成的函数可以直接这么用就不清楚了,可以去看一下jinjia2的内置函数。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1100 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:48 · PVG 02:48 · LAX 11:48 · JFK 14:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.