最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass a script to main.scala.html - Play! 2 - Stack Overflow

programmeradmin8浏览0评论

I am trying to pass javascripts specific to a page as a parameter to the main template. This is what I have tried:

main.scala.html:

@(title: String,moreScripts: Html)(content: Html)

<!DOCTYPE html>

<html>
    <head>
        <title>@title</title>
        <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
        <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
        <script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
        @moreScripts
    </head>
    <body>
        @content
    </body>
</html>

test.scala.html:

@main("Test",<script src="javascripts/test/test.js" type="text/javascript"></script>) {
        <h1>Test</h1>
}

But I am getting an type mismatch; found : scala.xml.Elem required: play.api.templates.Html error. I also tried wrapping the <script> statement in @{} to no avail.

How do I need to structure the <script> statement so that it is recognises the statement as HTML?

I am trying to pass javascripts specific to a page as a parameter to the main template. This is what I have tried:

main.scala.html:

@(title: String,moreScripts: Html)(content: Html)

<!DOCTYPE html>

<html>
    <head>
        <title>@title</title>
        <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
        <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
        <script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
        @moreScripts
    </head>
    <body>
        @content
    </body>
</html>

test.scala.html:

@main("Test",<script src="javascripts/test/test.js" type="text/javascript"></script>) {
        <h1>Test</h1>
}

But I am getting an type mismatch; found : scala.xml.Elem required: play.api.templates.Html error. I also tried wrapping the <script> statement in @{} to no avail.

How do I need to structure the <script> statement so that it is recognises the statement as HTML?

Share Improve this question edited May 31, 2012 at 3:11 travega asked May 31, 2012 at 2:59 travegatravega 8,41517 gold badges67 silver badges91 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

This has been discussed on the mailing list. Hope that helps.

@main{ <script src="javascripts/test/test.js" type="text/javascript"></script> } {
   <h1>Test</h1>
}

I struggled with this as well, but this seemed to work for me:

main.scala.html:

@(title: String, moreScripts: Html = Html(""))(content: Html)
<!DOCTYPE html>
<html>
  <head>
   <title>@title</title>
   @moreScripts
  </head>
  <body>
   @content
  </body>
</html>

test.scala.html:

@moreScripts = { <script src="routes.Assets.at("javascripts/MyJS.js")"></script>}

@main("Page Title", moreScripts) {
  <h1>Some content here</h1>
}

Of course, your Javascript file should be located at app/assets/javascripts as either a standard Javascript or Coffeescript file

发布评论

评论列表(0)

  1. 暂无评论