Manpreet
Manpreet

Reputation: 177

How to get the Base Url in Twig

I am using twig in Node+express. How can I get the base url in twig file ? I've tried the following 2 snippets:

{{ app.request.schemeAndHttpHost ~ app.request.requestUri }}


{{path()}}

Upvotes: 2

Views: 7780

Answers (2)

Paresh Barad
Paresh Barad

Reputation: 1609

I think npm twig package haven't any base URL common method(I have checked) so you need to pass it from your controller or route as per the following.

Route(route.js)

router.get('/twig', function (req, res) {
    res.render('index.twig', {
        message: "Hello World",
        baseUrl: req.protocol + '://' + req.get('host') + req.originalUrl
    });
});

views/index.twig

Base URL : <b>{{ baseUrl }}</b>

Upvotes: 2

Matteo
Matteo

Reputation: 39390

You could use the baseUrl attribute

{{ app.request.baseUrl }}

usually used with the scheme and host:

{{ app.request.schemeAndHttpHost ~ app.request.baseUrl }}

Hope this help

Upvotes: 5

Related Questions