添加< script>到< head>从Play Framework 2中的scala模板标签 [英] Add <script> to the <head> from scala template tags in Play Framework 2

查看:89
本文介绍了添加< script>到< head>从Play Framework 2中的scala模板标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从标签中将javascript添加到网页的<head>.

I would like to add javascript to the <head> of my webpage from within tags.

这是我在页面上使用的moreScripts等效项:

This is the moreScripts equivalent I'm using on my pages:

main.scala.html

@(title: String, scripts: Html = Html(""))(content: Html)
<!DOCTYPE html>
<html lang="nl">
    <head>
        <title>@title</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        @scripts
    </head>
    <body>
        @content
    </body>
</html>

page.scala.html

@scripts = {
    <script type="text/javascript">
        $(document).ready(function() {
            alert(1);
        });
    </script>
}
@main("Title", scripts) {
    <p>page content</p>
}

到目前为止一切顺利!但是,我想在我编写的标记(组件)中执行相同的操作,该标记需要在网页中包含一些javascript代码.

So far so good! However I want to do the same from within a tag (component) I've written that needs to include some javascript code into the webpage.

我的问题是如何将<script>元素从标记传递到main.scala.html?

My question is how can I pass a <script> element from the tag to the main.scala.html?

所以page.scala.html将是:

page.scala.html

@import tags._
@main("Title") {
    @mytag("green")
}

mytag.scala.html

@(color: String)
<script type="text/javascript">
    $(document).ready(function() {
        alert('@color');
    });
</script>

<p>Some more content</p>

在这种情况下,<script>标签呈现在HTML页面的一半,我想将<script>标签传递到@scripts变量中,以便可以在<head>标签内部呈现.

In this case the <script> tag is rendered halfway the HTML page, I want to pass the <script> tag into the @scripts variable so it can be rendered inside the <head> tag.

推荐答案

好的,我想出了一个更好的解决方案恕我直言.

Ok, I've come up with a nicer solution IMHO.

我创建了以下标签:

script.scala.html

@(content: Html)
@{
    var additionalScripts = ctx().args.get("additionalScripts").asInstanceOf[List[Html]];
    if(additionalScripts == null) {
        additionalScripts = new ArrayList[Html]();
        ctx().args.put("additionalScripts", additionalScripts)
    }

    val added = additionalScripts.add(content);
}

renderscripts.scala.html

@additionalScripts = @{ctx().args.get("additionalScripts").asInstanceOf[List[Html]]}
@if(additionalScripts != null) {
    @for(additionalScript <- additionalScripts) {
        @additionalScript
    }
}

在您的main.scala.html中,您可以使用:

In your main.scala.html you can use:

@(title: String)(content: Html)
@import tags._
<!DOCTYPE html>
<html lang="nl">
    <head>
        <title>@title</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        @renderscripts()
    </head>
    <body>
        @content
    </body>
</html>

您可以使用以下方法在模板或标签中指定其他脚本:

You can specify additional script(s) in your templates or tags using:

@import tags._
@script {
    <script type="text/javascript">
        $(document).ready(function() {
            alert('This will be in the head!');
        });
    </script>
}

这很好,对吗? :)

也许有人可以使用他们的Scala魔术来清理或增强我的代码:)

Maybe someone can clean up or enhance my code using their Scala magic :)

这篇关于添加&lt; script&gt;到&lt; head&gt;从Play Framework 2中的scala模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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