在 TWIG 中,是否可以获取包含 twig 变量的链接的绝对 url? [英] In TWIG, is possible to get absolute url of a link with twig variables in it?

查看:23
本文介绍了在 TWIG 中,是否可以获取包含 twig 变量的链接的绝对 url?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个如下所示的 URL:

{{domainID}}/action/{{userId}}/anotherAction

而后一个网址指向:

http://localhost/viewA/{{domainID}}/action/{{userId}}/anotherAction

但是,如果我尝试通过 iframe 从 viewB 加载 viewA,则 viewA 内的链接而不是指向:

http://localhost/viewA/{{domainID}}/action/{{userId}}/anotherAction

它将指向:

http://localhost/viewB/{{domainID}}/action/{{userId}}/anotherAction

如果跟随后者,用户将最终进入 404 页面.

我的问题是:

有没有办法在 twig 中获得以这种方式构建的 url 的绝对路径?

编辑

路由定义为:

@Route("/domain/details/{domainId}", name="domain_detailed_view")

我试图通过这种方式获取绝对路径:

{{ url({{domainID}}/action/{{userId}}/anotherAction) }}

但我收到此错误:

<块引用>

哈希键必须是带引号的字符串、数字、名称或表达式用括号括起来

解决方案

urlpath 函数采用路由名称,而不是路径.如果路由需要参数,您可以给它一个关联数组作为可选的第二个参数.

例如:

{{ url('domain_detailed_view', { 'domainId': domainId, 'userId': userId }) }}

http://symfony.com/doc/master/reference/twig_reference.htmlp>

I have several URLs that look as follows:

{{domainID}}/action/{{userId}}/anotherAction

And the latter URL points to:

http://localhost/viewA/{{domainID}}/action/{{userId}}/anotherAction

However, If I try to load viewA from viewB through an iframe, the link inside viewA instead of pointing to:

http://localhost/viewA/{{domainID}}/action/{{userId}}/anotherAction

it will point to:

http://localhost/viewB/{{domainID}}/action/{{userId}}/anotherAction

and the user will end up in a 404 page if it follows the latter.

My question is:

Is there anyway to get the absolute path of a url built that way in twig?

EDIT

The route definition is:

@Route("/domain/details/{domainId}", name="domain_detailed_view")

I tried to get the absolute path this way:

{{ url({{domainID}}/action/{{userId}}/anotherAction) }}

but I get this error:

A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses

解决方案

The url or path functions take the route name, not the path. You can give it an associative array as an optional second argument if the route requires parameters.

For example:

{{ url('domain_detailed_view', { 'domainId': domainId, 'userId': userId }) }}

http://symfony.com/doc/master/reference/twig_reference.html

这篇关于在 TWIG 中,是否可以获取包含 twig 变量的链接的绝对 url?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆