0

I want to get the value of an input field from my template twig with twig not with javascript. My input field hasn't a form, it is a hidden input.

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

I want to do this:

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

How can I get value_Of_Hidden_Input

EDIT:

My requirement:

I have a list of customers and I have for each customer a button to show details of customer "Modifier coordonnées". I want on clicked one customer a function AJAX will executing the action showAction This is my code:

{% 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 %}

My problem is how can determined value_of_id_client?

4
  • Where is the input-field and especially its value conning from? Is it a form row, like {{ form_row(form.id_client) }}? Commented Nov 20, 2014 at 13:18
  • No, I don't have a form. When I clicked at a button, the value of my hidden input changes to get the value of my client cliqued with javascript Commented Nov 20, 2014 at 13:27
  • Can you explain more detailed what you are trying to achieve exactly? Commented Nov 20, 2014 at 13:36
  • 1
    @insertusernamehere I edit my post to explaine the original problème Commented Nov 20, 2014 at 13:52

5 Answers 5

4

try this .

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

Have more information on http://symfony.com/doc/current/reference/forms/twig_reference.html

Sign up to request clarification or add additional context in comments.

1 Comment

OP is asking to pass a front-end Javascript variable into a route. While this may work for a submitted form with POST data, you can't pass Javascript variables through Twig as Twig is already rendered by the time you need to do that.
1

First of all: An id must be unique throughout the whole document. If you have multiple targets use the class-attribute instead. I also added the current url including the corresponding id to each entity in your loop, using the HTML5 data-attribute:

{% 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 %}

In your JavaScript part, you have to change the selector and retrieve the url of the currently clicked element:

$('.handler').click(function() {
    var href = $(this).data('href');

    $.ajax({
        […]
        url: href,
        […]
    ));
});

9 Comments

I didn't see the <input>-element in your detailed code example. So this solution works without an extra <input>-element.
This sort of works, until the Symfony route is something other than /path/to/route/{id}. It could end up being something like /object/{id}/edit.
but url: '{{ path('client_show'}}' + id, dont work correctly
@Aminesrine I've fixed his answer with a tiny edit for a typo - it may also be missing a slash, like '{{ path('client_show') }}/' + id', but I implore you to also try the FOSJsRoutingBundle idea
@sjagr Thanks - I overlooked that brace. :)
|
0

You need to use FOSJsRoutingBundle to generate a proper Symfony path that takes in an argument and places it in the correct portion of the URL (depending on your routes, your variable portion may be somewhere in the middle of the URI!)

You must follow all the installation instructions for the bundle, and make sure you expose the route in question.

Then you pull the value into the Routing.generate function by passing the value retrieved by jQuery:

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

5 Comments

That's a nice bundle. :)
I try to do it with this bundle but this error is occured: ReferenceError: goog is not defined in router.js "goog.provide('fos.Router');" But I resolved it by: var id = $(this).data('id'); var route = "{{ path('client_show', { 'if': "PLACEHOLDER" }) }}"; route = route.replace("PLACEHOLDER", id); et puis url : route,
@Aminesrine Did you do php app/console assets:install --symlink web? Are you loading the router.js with <script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>? If you're using the annotation method, you must use @FOSJsRoutingBundle/Resources/public/js/router.js and not @FOSJsRoutingBundle/Resources/js/router.js
I tried it with th annotation and the @FOSJsRoutingBundle/Resources/public/js/router.js, but this error is occured : The route "client_show" does not exist.
@Aminesrine Now you have to expose your route (see the options: expose: true annotation? Use that!) Don't forget to php app/console cache:clear --env=prod
-1

You can't do this that way, try(js):

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

Comments

-1

I think the correct way to handle this is in the controller.

When you post the form, take that hidden value and inject it into your template.

Pull id_client from the Request object.

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

Then in your template, you can use the client_id variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.