Symfony2,用Twig获取输入字段的值



我想用树枝而不是javascript从我的模板树枝中获取输入字段的值。我的输入字段没有表单,它是一个隐藏的输入。

{% block body -%}
    <input type="hidden" name="id_client" id="id_client" value="123"/>
    ...
{% endblock %}

我想这样做:

{{ path('client_view',{"id": value_Of_Hidden_Input})  }}

我怎样才能获得value_Of_Hidden_Input

编辑:

我的要求:

有一个客户列表,我为每个客户提供了一个按钮来显示客户"Modifier coordonnées"的详细信息。我想在点击一个客户时,一个函数 AJAX 将执行动作显示动作这是我的代码:

{% block body -%}
<div class="tab-client">
      <table id="mytable" class="table table-bordred table-striped" cellpadding="0" cellspacing="0">
      <thead>
          <tr>
              <th>Raison Sociale</th>
              <th>Identifiant</th>
              <th>M. de passe</th>
              <th>Solde actuel</th>
              <th class="text-center sty-td-action">Action</th>
          </tr>
      </thead>
      <tbody>
          {% for entity in entities %}
              <tr>
                  <td>{{ entity.raisonSociale}} </td>
                  <td>{{ entity.login }}</td>
                  <td>{{ entity.password }}</td>
                  <td>{{ entity.soldeSMS }}</td>
                  <td>
                      <a class="modifier-client" id="modif_coordonnee"><span class="glyphicon glyphicon1">Modifier coordonnées</span></a>
                      <a href="#historique" class="modifier-client"><span class="glyphicon glyphicon2">Voir Historique</span></a>
                      <a href="#ajout" class="modifier-client"><span class="glyphicon glyphicon3">Ajout transaction</span></a>
                  </td>
              </tr>
          {% endfor %}
      </tbody>
      </table>
</div><!--tab-client-->
{% endblock %}
{% block javascripts %}
    <script>
         $("#modif_coordonnee").click(function() {
                $.ajax({

                    //On lui indique le type d'envoie des informations
                    type: 'POST',
                    //On lui indique le chemin de la fonction
                    url:  '{{ path('client_show',{"id": value_of_id_client})  }}',

                    //On lui donne la valeur du choix qu'on a fait, et id est la variable qui va contenir notre valeur, nous la retrouvons dans notre controller


                    //Enfin nous lui disons de remplir notre formulaire avec le resultat  
                    success: function(response)
                    {
                        ......

                    }
                }
            )});
    </script>
{% endblock %}

我的问题是如何确定value_of_id_client

试试这个。

{{ path('client_view',{"id": form.name_field_hidden.vars.value})  }}

了解有关 http://symfony.com/doc/current/reference/forms/twig_reference.html 的更多信息

首先:id在整个文档中必须是唯一的。如果您有多个目标,请改用 class -属性。我还使用 HTML5 data -attribute 添加了当前url,包括循环中每个实体的相应id

{% for entity in entities %}
    […]
    <a class="modifier-client handler" data-href="{{ path('client_show',{ 'id': entity.id }) }}">
        <span class="glyphicon glyphicon1">Modifier coordonnées</span>
    </a>
    […]
{% endfor %}

在 JavaScript 部分中,您必须更改选择器并检索当前单击的元素的 url:

$('.handler').click(function() {
    var href = $(this).data('href');
    $.ajax({
        […]
        url: href,
        […]
    ));
});

你需要使用FOSJsRoutingBundle来生成一个正确的Symfony路径,该路径接受一个参数并将其放置在URL的正确部分(根据您的路由,您的变量部分可能位于URI的中间!

您必须遵循捆绑包的所有安装说明,并确保公开有问题的路由。

然后,通过传递 jQuery 检索到的值,将值拉入 Routing.generate 函数中:

url:  Routing.generate('client_show', {id: $('#id_client').val()}),
你不能

那样做,try(js):

{{path('client_view')}}/+$("#id_client").val()

我认为处理此问题的正确方法是在控制器中。

发布表单时,获取该隐藏值并将其注入模板中。

从请求对象中提取id_client。

$this->render('TEMPLATE', array('client_id'=>$request->query->get('id_client'));

然后在模板中,您可以使用 client_id 变量。

最新更新