如何将原始 html 传递给 Play 框架视图? [英] How to pass raw html to Play framework view?

查看:26
本文介绍了如何将原始 html 传递给 Play 框架视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个简单的 URL 传递给播放框架应用程序中的视图,但是当作为字符串传递时,URL 中的 & 更改为 & 导致 URL 不工作.

如果我将参数更改为 Html,即 @(url: Srting)@(url: Html),那么当我尝试传递时会出错将 url 作为字符串传递给 view.render() 方法.

如何将 url 转换为 Html 并以此方式传递?

解决方案

为了防止视图上动态内容发生的默认转义,您需要将 String@Html(String) 函数:

查看:

@(url: String)<div class="myLink">转到:@Html(url) 
不要:@url

控制器:

public static 结果 displayLink(){return ok(view.render("<a href='http://stackoverflow.com/'>Stack Overflow</a>"));}

请参阅文档中的模板引擎页面了解更多信息(特别是最底部的转义"部分).

I'm trying to pass a simple URL to a view within a play framework app, however when passed as a string, the & in the url is changed to &amp; which causes the URL to not work.

If I change the argument to Html, i.e @(url: Srting) to @(url: Html), then I get an error when I try to pass the url as string to view.render() method.

How can I convert the url into Html and pass it as that?

解决方案

To prevent the default escaping that happens for dynamic content on views you need to wrap the String with @Html(String) function:

View:

@(url: String)
<div class="myLink">
   Go to: @Html(url) <br>
   not to: @url
</div>

Controller:

public static Result displayLink(){
   return ok(view.render("<a href='http://stackoverflow.com/'>Stack Overflow</a>"));
}

See The template engine page on the documentation for more info (specifically the "Escaping" section at the very bottom).

这篇关于如何将原始 html 传递给 Play 框架视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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