本文介绍了在树枝模板中设置多维数组中单个对象的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于项目需求,我需要在渲染之前更改一些表单字段数据.为此,我遍历表单元素并动态更改所需的值.
For project needs I need to change some form fields data before they render. To do that I iterate over form elements and dynamically change values I need.
问题是我不能改变价值.我收到此错误:
Problem is that I can't change value. I got this error:
Unexpected token "punctuation" of value "." ("end of statement block" expected).
我使用这段代码来更改值,但是上面出现错误:
I used this piece of code to change value but I got error above:
{% set arr = arr|merge({'element': 'value'}) %}
有人知道问题出在哪里吗?
Does someone knows where is the problem?
这是我使用的代码.
Twig模板代码(我使用的示例代码)
Twig template code (example code I used)
<ul>
{% dump (edit_form) %}
{% for element in edit_form.children %}
{% dump (element.vars) %}
{% set element.vars = element.vars|merge({'name': 'My title (just for testing purpose)'}) %}
<li>{{ element.vars.name }}</li>
{% endfor %}
</ul>
表单对象:
FormView {#637 ▼
+vars: array:24 [▶]
+parent: null
+children: array:4 [▼
"title" => FormView {#699 ▼
+vars: array:24 [▼
"value" => "le title"
"attr" => []
"form" => FormView {#699}
"id" => "adminbundle_intro_title"
"name" => "title"
"full_name" => "adminbundle_intro[title]"
"disabled" => false
"label" => null
"label_format" => "admin.intro.form.%name%"
"multipart" => false
"block_prefixes" => array:3 [▶]
"unique_block_prefix" => "_adminbundle_intro_title"
"translation_domain" => null
"cache_key" => "_adminbundle_intro_title_text"
"errors" => FormErrorIterator {#696 ▶}
"valid" => true
"data" => "le title"
"required" => true
"size" => null
"label_attr" => []
"compound" => false
"method" => "POST"
"action" => ""
"submitted" => false
]
+parent: FormView {#637}
+children: []
-rendered: false
}
"content" => FormView {#698 ▶}
"isEnabled" => FormView {#702 ▶}
"_token" => FormView {#711 ▶}
]
-rendered: false
}
推荐答案
要更改数组的某个索引,最好通过扩展Twig
来解决,
To change a certain index of an array you're best of with extending Twig
, a solution could be this,
namespace Your\Namespace;
class ProjectTwigExtension extends Twig_Extension {
public function getFunctions() {
return array(
new Twig_SimpleFunction('set_array_value', array($this, 'setArrayValue'), ['needs_context' => true,]),
new Twig_SimpleFunction('set_object_property', array($this, 'setArrayValue'), ['needs_context' => true,]),
);
}
public function setArrayValue(&$context, $array_name, $index, $value) {
if (!isset($context[$array_name])) return;
if (is_array($context[$array_name])) $context[$array_name][$index] = $value;
elseif(is_object($context[$array_name])) {
if (method_exists($context[$array_name], $index)) $context[$array_name]->{$index}($value);
elseif(method_exists($context[$array_name], 'set'.$index)) $context[$array_name]->{'set'.$index}($value);
}
}
public function getName() {
return 'ProjectTwigExtension';
}
}
$twig->addExtension(new \Your\Namespace\ProjectTwigExtension());
/** ... code ... **/
$user = new User();
$user->setUserName('admin');
$twig->render('template.twig', [ 'somearray' => ['foo' => 'bar',], 'user' => $user, ]);
{{ dump(somearray) }} {# output: array(1) { ["foo"]=> string(3) "bar" } #}
{{ set_array_value('somearray', 'foo', 'foobar') }}
{{ dump(array) }} {# output: array(1) { ["foo"]=> string(6) "foobar" } #}
{{ dump(user.getUserName()) }} {# output: string(5) "admin" #}
{{ set_object_property('user', 'UserName', 'StackOverflow') }}
{{ dump(user.getUserName()) }} {# output: string(13) "StackOverflow" #}
这篇关于在树枝模板中设置多维数组中单个对象的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!