如何将 PHP 代码转换为 Twig 代码? [英] How to convert PHP code to Twig code?
本文介绍了如何将 PHP 代码转换为 Twig 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在尝试将一些代码添加到我已经在 PHP 中使用的 Twig 文件中.
I am currently trying to add some code to Twig file that I already use with PHP.
有一个从 PHP 到 Twig 的在线转换器,但它不能完成这项工作.这是我用这个转换器得到的,转换:
There is an online converter from PHP to Twig, but it doesn't do the job. Here is what I get with this converter, converting:
{ if($language['code'] == 'bg') {$cur_ = 'BGN';} else {$cur_ = 'USD'; }?>
为此:
{% if language.code == 'bg' { cur_ = 'BGN' } else { cur_ = 'USD' }}
还有这个:
onclick="$('input[name=\'code\']').attr('value', '<?php echo $language['code']; ?>');$('input[name=\'currency_code\']').attr('value', '<?php echo $cur_; ?>'); $(this).parent().parent().submit();"
为此:
onclick="$('input[name=\'code\']').attr('value', '{{ language.code }} ');$('input[name=\'currency_code\']').attr('value', '{{ cur_ }} '); $(this).parent().parent().submit();"
但我不明白到底应该怎么做才能让它发挥作用.
But I can't understand what exactly should do to make it work.
推荐答案
对于第一行,使用 set
定义一个变量:
For the first line, use set
to define a variable:
{% if language.code == 'bg' %}
{% set cur_ = 'BGN' %}
{% else %}
{% set cur_ = 'USD' %}
{% endif %}
更好的是,使用三元运算符:
{% set cur_ = (language.code == 'bg') ? 'BGN' : 'USD' %}
对于第二个,只需替换 <?php echo $...;?>
by {{...}}
:
For the second, just replace <?php echo $...; ?>
by {{...}}
:
onclick="$('input[name=\'code\']').attr('value', '{{ language.code }}');$('input[name=\'currency_code\']').attr('value', '{{ cur_ }}'); $(this).parent().parent().submit();"onclick="$('input[name=\'code\']').attr('value', '{{ language.code }}');$('input[name=\'currency_code\']').attr('value', '{{ cur_ }}'); $(this).parent().parent().submit();"
这篇关于如何将 PHP 代码转换为 Twig 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文