[2.0] How to pass a script to main.scala.html?

654 views
Skip to first unread message

Fabiano Felipe

unread,
Mar 17, 2012, 9:56:32 PM3/17/12
to play-fr...@googlegroups.com


In version 1.2.4 I did like this:

# {set 'moreScripts'}
# {script src = "myscript.js" /}
# {/ set}

how to do this in version 2?

Freewind

unread,
Mar 17, 2012, 10:23:03 PM3/17/12
to play-fr...@googlegroups.com
Define a parameter in `main.scala.html`:

    @(moreScripts: Html)(content:Html)

Then, in your inner pages, you should pass the scripts as:

    @main(<script src="myscript.js"></script>) {

Fabiano Felipe

unread,
Mar 17, 2012, 10:30:57 PM3/17/12
to play-fr...@googlegroups.com
thanks!

Mark Waddle

unread,
Mar 17, 2012, 11:26:01 PM3/17/12
to play-fr...@googlegroups.com
That is a good pattern for that scenario, thanks. Do you know of one for views being called inside the main { ... html ... @myView ... html ... }?

Andy Scott

unread,
May 30, 2012, 11:47:52 PM5/30/12
to play-fr...@googlegroups.com
I have tried almost exactly what @Freewind suggested but keep getting the error: type mismatch; found : scala.xml.Elem required: play.api.templates.Html

What am I doing wrong?

My code:

main.scala.html:

@(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(<script src="javascripts/test/test.js" type="text/javascript"></script>) {
       
<h1>Test</h1>
}

Any ideas?

Durgesh Mankekar

unread,
May 31, 2012, 12:20:39 AM5/31/12
to play-fr...@googlegroups.com
Have you tried the following (curly braces instead of parenthesis)?

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

Leonard Punt

unread,
May 31, 2012, 3:23:23 AM5/31/12
to play-fr...@googlegroups.com
You need to pass a play.api.templates.Html class, as the error says. So you need to cast your 'script element' to the Html class. You can do this by adding curly brackets around it, like this:

Durgesh Mankekar

unread,
May 31, 2012, 12:06:16 AM5/31/12
to play-fr...@googlegroups.com
Have you tried the following (curly braces instead of parenthesis)? 

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

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/rw11mYI2pvEJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Andy Scott

unread,
May 31, 2012, 4:36:23 PM5/31/12
to play-fr...@googlegroups.com
Awesome thanks guys...
Reply all
Reply to author
Forward
0 new messages