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'

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

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

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

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

推荐文章

  • 证书验证失败:无法获得本地颁发者证书
  • 当使用pip3安装包时,“Python中的ssl模块不可用”
  • 无法切换Python与pyenv
  • Python if not == vs if !=
  • 如何从scikit-learn决策树中提取决策规则?
  • 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
  • 将旋转的xtick标签与各自的xtick对齐
  • 为什么元组可以包含可变项?
  • 如何合并字典的字典?
  • 如何创建类属性?
  • 不区分大小写的“in”
  • 在Python中获取迭代器中的元素个数
  • 解析日期字符串并更改格式
  • 使用try和。Python中的if
  • 如何在Python中获得所有直接子目录

aliyun

最新文章

  • 如何删除默认的导航栏空间在SwiftUI导航视图
  • 我可以列出所有的标准Go包吗?
  • 证书验证失败:无法获得本地颁发者证书
  • 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
  • 打开同一目录两次
  • 实体框架核心:在上一个操作完成之前,在此上下文中开始的第二个操作
  • NullInjectorError: AngularFirestore没有提供程序
  • 如何为构造函数定制Visual Studio的私有字段生成快捷方式?
  • 从元组/数组值派生联合类型
  • 如何在Angular2中截断文本?
  • 安装tzdata非交互式
  • ionic 2 - Error在Android Studio中找不到Gradle的安装版本
  • 如何查看所有地区所有正在运行的Amazon EC2实例?
  • 当使用pip3安装包时,“Python中的ssl模块不可用”
  • 安装APK时出现错误

标签

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