[英]Cycle through and pull information from array in Twig
我目前有一個充滿預先填充的表單字段的數組:
$fields = array('title','first_name')
$info = array(
'title' => 'Mr',
'first_name' => 'John',
'last_name' => 'Smith'
)
如您所見,此特定字段數組僅包含標題和名字。
我的目標是循環遍歷fields數組,看看我的$info
數組中是否有任何信息預先填充該字段。
就像是:
foreach (fields as field) {
if (field is in $info array) {
echo the_field_value;
}
}
但顯然在Twig,目前我有類似的東西:
{% for key, field in context.contenttype.fields %}
{% if key in context.content|keys %} << is array
{{ key.value }}<< get the value of the field
{% endif %}
{% endfor %}
任何幫助是極大的贊賞。
這個例子轉儲你需要的東西:
{% set fields = ['title','first_name'] %}
{% set info = { 'title': 'Mr', 'first_name': 'John', 'last_name': 'Smith' } %}
{% for key in fields %}
{% if key in info|keys %}
{{ info[key] }}
{% endif %}
{% endfor %}
結果:
約翰先生
這里的工作解決方案: http : //twigfiddle.com/i3w2j3
希望這有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.