5

I'm using Symfony 2.0.19. I'm trying to create a hyperlink to an external URL, which is retrieved from a database.

I tried doing this

<td><a href="{{dominio.url}}">{{dominio.url}}</a></td>

but the path I get is a relative path to the URL inside the base URL example "localhost/web/www.tralalalala.com" instead of just "www.tralalalala.com".

How do I do this?

nbro
  • 15,395
  • 32
  • 113
  • 196
Splendonia
  • 1,329
  • 3
  • 37
  • 59

3 Answers3

9

Here's a concrete example of what Pierrickouw is suggesting:

Create a Twig extension or filter under src/Twig, and call it for example ExternalLinkFilter. Define the class as follows:

<?php 

namespace AppBundle\Twig;

class ExternalLinkFilter extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('external_link', array($this, 'externalLinkFilter')),
        );
    }

    /* source: http://stackoverflow.com/a/2762083/3924118 */
    public function externalLinkFilter($url)
    {
        if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
            $url = "http://" . $url;
        }

        return $url;
    }

    public function getName()
    {
        return 'external_link_filter';
    }
}

?>

Now, you should register this class as a service in config/services.yml as follows:

services:

    # other services

    app.twig.external_link:
        class: AppBundle\Twig\ExternalLinkFilter
        public: false
        tags:
            - { name: twig.extension }

Now you can simply use the filter called external_link as you would use any Twig's default one, e.g.:

...

<a href="{{check.hostname | external_link }}"> {{check.hostname}}</a>

...
nbro
  • 15,395
  • 32
  • 113
  • 196
3

I suggest you to build your own Twig filter.

If your url aldready have http://, don't add it, otherwise, add it.

Check here for how-tos.

Pierrickouw
  • 4,644
  • 1
  • 30
  • 29
0

A symfony 5 version of nbro's answer will be: (This will take care of all kind of url that is read from the db)

source: https://symfony.com/doc/current/templating/twig_extension.html

in src/Twig create the file

    <?php 

    namespace App\Twig;

    use Twig\Extension\AbstractExtension;
    use Twig\TwigFilter;

    class ExternalLinkFilter extends AbstractExtension
    {
        public function getFilters()
        {
            return array(
                new TwigFilter('external_link', array($this, 'externalLinkFilter')),
            );
        }

        /* source: http://stackoverflow.com/a/2762083/3924118 */
        public function externalLinkFilter($url)
        {
            if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
                $url = "http://" . $url;
            }

            return $url;
        }

        public function getName()
        {
            return 'external_link_filter';
        }
    }

    ?>

You don't need to register it as a service. Symfony 5 will notice the presence of that Twig extension

Use it in twig template as suggested by @nbro

    <a href="{{check.hostname | external_link }}"> {{check.hostname}}</a>
Josiah
  • 118
  • 10