简体   繁体   English

如何通过Twig中的脚本添加链接路径?

[英]How to add path to link via script in Twig?

I'm adding a script into my twig file, to redirect to other route. 我在我的twig文件中添加了一个脚本,以重定向到其他路径。

<script>
$('#lnkPanel').click(function(e) {
    e.preventDefault();
    window.location = '{{ path('profile/' ~ user.id ~ '') }}';
});

But when my twig is rendering I get an error and this not show my page. 但是当我的枝条渲染时,我得到一个错误,这不显示我的页面。 Error is: "An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "profile/{1}" as such route does not exist.")" 错误是:“在呈现模板期间抛出异常(”无法生成指定路径的URL“配置文件/ {1}”,因为此类路由不存在。“)”

How can I do to not get an error while is rendering twig, and show my page correctly?? 如何在渲染树枝时不会出错,并正确显示我的页面?

Thanks! 谢谢!

If you are using SlimnViews TwigExtension you should be able to get it running like 如果您正在使用SlimnViews TwigExtension您应该能够像它一样运行它

<script>
$('#lnkPanel').click(function(e) {
    e.preventDefault();
    window.location = '{{ siteUrl('/profile/') }}{{user.id|e}}/';
});
</script>

Replace {{user.id|e}} with the Twig variable you have for user's profile ID. {{user.id|e}}替换为您对用户个人资料ID的Twig变量。

在twig path函数中,它应该是您的路径名称:

window.location = '{{ path('profile_route_name', {id: user.id}) }}';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM