在scala模板中解码html

时间:2012-05-18 20:05:48

标签: templates scala playframework html-escape-characters

我正在尝试制作一个基于路线行为不同的标签,但是当我尝试在@{ ... }块内渲染html时,我似乎无法正确插入变量。

@(route: String, on_image: String, off_image: String, alt_text: String)

@{
    val Pattern = ("(" + route + ".*)").r
    val path = request().path()


    path match {
        case Pattern(foo) => {
            if(path != route && route == "/") {
                <script type="text/javascript">
                    alert("{route}");
                </script>
            } else {
                <script type="text/javascript">
                    alert("{route}");
                </script>           
            }
        }
        case _ => {
            <script type="text/javascript">
                alert("{route}");
            </script>       
        }
    }
}

当我加载页面时,我在Firebug控制台中出现语法错误,其中显示脚本标记的内容评估为:

alert(&quot;/accounts&quot;);

现在,我已尝试围绕整个块@Html{ ... }@Html( ... )以及@Html({ ... })等内容。我也尝试了alert(@Html("{bar}"));和其他类似的东西,但是一切都会产生各种错误。

这样做的正确方法是什么?

修改

更新为显示为了简单起见只尝试使用一个变量,并显示该变量的来源。

1 个答案:

答案 0 :(得分:1)

解决了一点间接问题。

@(route: String, on_image: String, off_image: String, alt_text: String)

@on = {
    <a href="@route"><img src="@routes.Assets.at(on_image)" alt="@alt_text" /></a>
}

@off = {
    <a href="@route"><img src="@routes.Assets.at(off_image)" alt="@alt_text" /></a>
}

@{
    val Pattern = ("(" + route + ".*)").r
    val path = request().path()


    path match {
        case Pattern(foo) => {
            if(path != route && route == "/") {
                {off}
            } else {
                {on}    
            }
        }
        case _ => {
            {off}       
        }
    }
}