Twig template external url img

2.6k views Asked by At

I am trying to display image from facebook profile.

routing.yml

_graph_facebook:
path: graph.facebook.com/{fbId}/{var}
requirements:
    _scheme:  https

template.html.twig

What I get is htpps://mydomain.local/graph.facebook.com/facebookId/picture .

I was trying with assets but it works only if it's hardcoded.

{% image 'https://graph.facebook.com/'~app.user.facebookId~'/picture'%}
        <img src="{{ asset_url }}" alt="Example" />
{% endimage %}

This code doesn't work it says that: Unexpected token "operator" of value "~". I couldn't find answer probably it's vaery simply.Thanks for Your help

3

There are 3 answers

1
A.L On BEST ANSWER

As said in the comments, the router only applies to the URLs of your website.

Anyway, you can simply display the image with the facebookId value:

<img src="https://graph.facebook.com/{{ app.user.facebookId }}/picture" alt="Example" />
0
Fabien MEYNARD On

You can't use routing for external URI management only internal.

If you want to encapsulate this "behavior" in a method/function accessible by twig i can suggest you to create a Twig_Method via an custom twig extension : http://symfony.com/doc/current/cookbook/templating/twig_extension.html

0
Dmitriy Sintsov On

Concatenation operator's ~ arguments should be space-char separated.

'https://graph.facebook.com/' ~ app.user.facebookId ~ '/picture'

External urls should be escaped with html_attr filter:

<img src="{{ asset_url|e('html_attr') }}" alt="Example" />