[英]How can I display a variable with twig via ajax?
I have a function that is loading a form via Ajax on my page. 我有一个通过页面上的Ajax加载表单的函数。
mypage.html.twig: mypage.html.twig:
function forms(e,el) {
var id = $(el).attr("data-id");
var target = $(el).attr("data-target");
e.preventDefault();
var $link = $(e.currentTarget);
$.ajax({
method:'POST',
data: {
"id": id,
"target": target
},
url: $link.attr('href')
})
}
$('.create-item').on( 'click', function (e) {
forms(e,this);
});
MyController.php MyController.php
$response = new JsonResponse(
array(
'message' => 'Success',
'output' => $this->renderView('form.html.twig',
array(
'entity' => $item,
'form' => $form->createView(),
))), 200);
return $response;
This is the form that is loaded (form.html.twig): 这是加载的表单(form.html.twig):
<section class="content-header" style="margin-bottom:20px">
<h1 style="float:left;margin-bottom:30px">{{ target }}</h1>
</section>
<section class="content" style="clear:left">
<div class="form-group">
{{ form_start(form) }}
{{ form_end(form) }}
</section>
The form is loaded correctly. 表单已正确加载。 But in the
<h1></h1>
area I want to load the variable target
. 但是在
<h1></h1>
区域中,我想加载变量target
。
It is not working, I get an error message: 它不起作用,我收到一条错误消息:
Variable "target" does not exist.
变量“目标”不存在。
You are not passing the posted value target
to your view 您没有将过帐值
target
传递给视图
$response = new JsonResponse(
array(
'message' => 'Success',
'output' => $this->renderView('form.html.twig',
array(
'entity' => $item,
'form' => $form->createView(),
'target' => $target, //<-----
))), 200);
return $response;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.