1

I have this code:

href="{{ path('new') }}"

Now is necessary use one variable in this section:

href="{{ path(item.ruta) }}"

But this show a error:

An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "" as such route does not exist.").

How can solution this problem?

5
  • It seems that item.ruta is empty, so no route could be generated. You could specifiy a fallback like this {{ path(item.ruta ? item.ruta : 'new') }} Commented Feb 4, 2019 at 15:07
  • Thanks Fabian. Now show this error with this code: "An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "#" as such route does not exist.")." Commented Feb 4, 2019 at 15:10
  • It is not a solution, the item.ruta is inside a loop. The idea is to assign the content of item.ruta within path Commented Feb 4, 2019 at 15:20
  • What do you mean with The idea is to assign the content of item.route within path ? Commented Feb 4, 2019 at 15:21
  • 1
    Sorry Fabian, is perfect your solution, thanks. Commented Feb 4, 2019 at 15:35

1 Answer 1

4

It seems that item.ruta is empty, so no route could be generated.

You could specifiy a fallback like this {{ path(item.ruta ? item.ruta : 'new') }} or if you want to stay on the current page you need to do something like descripted here: get current url in twig template?

{% if item.ruta %}
    href="{{ path(item.ruta) }}"
{% else %}
    href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}"
{% endif %}

or if you really want # only, then remove the path function call

{% if item.ruta %}
    href="{{ path(item.ruta) }}"
{% else %}
    href="#"
{% endif %}
Sign up to request clarification or add additional context in comments.

1 Comment

<a href="{{ item.ruta ? item.ruta : '#' }}">...</a>

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.