Code
  • 首页
  • cpu-architecture
  • performance
  • c++
  • branch-prediction
  • java
2024-10-06 05:00:05

金句2条件速记

pythonconditional-operatorjinja2

假设我有这个:

{% if files %}
    Update
{% else %}
    Continue
{% endif %}

在PHP中,我可以写一个条件简写,比如:

<?php echo $foo ? 'yes' : 'no'; ?>

是否有一种方法,我可以翻译这个工作在一个jinja2模板:

'yes' if foo else 'no'

当前回答

另一种方式(但不是python风格。这是JS风格)

{{ files and 'Update' or 'Continue' }}
2019-11-20 12:13:07

其他回答

另一种方式(但不是python风格。这是JS风格)

{{ files and 'Update' or 'Continue' }}
2019-11-20 12:13:07

是的,可以使用内联if-表达式:

{{ 'Update' if files else 'Continue' }}
2013-01-08 12:32:08

推荐文章

  • 如何激活蟒蛇环境
  • 省略[…]意思是在一个列表里?
  • 为什么我得到“'str'对象没有属性'读取'”当尝试使用' json。载入字符串?
  • 不区分大小写的列表排序,没有降低结果?
  • 排序后的语法(key=lambda:…)
  • 在烧瓶中返回HTTP状态代码201
  • 使用python创建一个简单的XML文件
  • APT命令行界面式的yes/no输入?
  • 如何打印出状态栏和百分比?
  • 在Python中获取大文件的MD5哈希值
  • 在Python格式字符串中%s是什么意思?
  • 如何循环通过所有但最后一项的列表?
  • python用什么方法避免默认参数为空列表?
  • ValueError: numpy。Ndarray大小改变,可能表示二进制不兼容。期望从C头得到88,从PyObject得到80
  • Anaconda /conda -安装特定的软件包版本

aliyun

最新文章

  • 在Swift中如何调用GCD主线程上的参数方法?
  • 在文本文件中创建或写入/追加
  • 如何设置身体高度溢出滚动
  • 如何在Makefile中设置子进程的环境变量
  • 我如何告诉Spring Boot哪个主类用于可执行jar?
  • 如何将Java8流的元素添加到现有的列表中
  • 在Java 8中是否可以转换流?
  • GitHub -致命:无法读取用户名https://github.com':没有这样的文件或目录
  • 我如何在一个片段中访问getSupportFragmentManager() ?
  • 如何激活蟒蛇环境
  • 窗口。亲近与自我。close不关闭Chrome中的窗口
  • 并发HashSet<T>在。net框架?
  • 从控制器内获得控制器和动作名称?
  • 我如何渲染一个列表选择框(下拉)与引导?
  • 省略[…]意思是在一个列表里?

标签

cpu-architectureperformancec++branch-predictionjavaundogit-commitgitversion-controlgit-remotegit-pushgit-branchgit-pullgit-fetchpythongeneratoriteratorjsonmime-typescontent-typearraysjavascriptgit-addcode-formattingcstandards-complianceoperatorscommentsdynamic-memory-allocationstackmemory-managementheap-memorylanguage-agnosticoverwritebackground-colorbrowserhtmldomjqueryvisibilityuse-strictsyntaxjslintremote-branchgit-checkoutpython-moduleidiomsprogram-entry-pointnamespacesbranch
2025 code 京ICP备15047053号-1