poppies
poppies

Reputation: 120

Set url twig by value Jquery

I'm using Symfony and twig template. My code:

var dataId = $('.url-select').data('id');
url = "{{ url('product', {id: 'dataId'}) }}";

It not working.Do you have some solution, if this is possible?

Upvotes: 0

Views: 494

Answers (1)

bechir
bechir

Reputation: 449

This short article answer to your question: https://symfony.com/doc/current/frontend/encore/server-data.html

In your twig template:

<div class="url-select"
     data-id="{{ product.id }}"
     data-url="{{ path('product', {id: product.id}) }}">
</div>

And in js you can just select the url by doing this:

var id = $('.url-select').data('id')
var url = $('.url-select').data('url')

Upvotes: 2

Related Questions