Reputation: 177
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
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
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