Play! 2.1.1 javascript framework in the template causes a compilation error

I just started working with the framework. Love it, in addition to having problems with presentation templates.

Whenever I include javascript directly in the presentation template, I get a compilation error. Is it not possible with the game! templated?

@(title: String)(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.9.0.min.js")" type="text/javascript"></script> </head> <body> <script> function isEmpty(obj) { //for(var prop in obj) { //if(obj.hasOwnProperty(prop)) try{ if(JSON.stringify(obj)=='{}'){ return true; }else{ return false; } }catch(e){ return false; } } </script> 

Am I getting a "Not Parsed" error message? on line with function isEmpty (obj) {

Thanks in advance.

+4
source share
4 answers

Even if you are commenting out a JavaScript string using // for the parser template, this is a special char:

 //for(var prop in obj) { 

In general, brackets in Scala templates are of particular importance, so creating advanced JavaScripts directly in the view causes a lot of problems. You still need to control if a new innocent change in JS does not block the template parser, or vice versa - if the template analyzer does not destroy your scripts. The simplest solution is to simply separate JS from the view, you have two solutions, and I think it's better first:

  • Save your JS in a shared *.js file and include it with the <script src...> . If you need to pass some data from your view, first use the global JS var:

     <script>var helloMessage = "Welcome on page @title";</script> <script src="/path/to/your.js"></script> 
  • Read the contents of the JS file (not the Scala template!) In your controller and pass it as a String , you will need to avoid it with Html() :

     @(title: String, mySpecialJS: String)(content: Html) <!DOCTYPE html> <html> <head> <title>@title</title> @Html(mySpecialJS) </head> <body> 
  • You can also join both approaches, for example, if you want to pass a complex object to JavaScript depending on the controls / views, you can also build a JSON object in the controller, convert it to String , and then enable JS with a common tag:

     @(title: String, myJsConfig: String)(content: Html) <!DOCTYPE html> <html> <head> <title>@title</title> <script>var myJsConfig = @Html(myJsConfig)</script> <script src="/path/to/your.js"></script> </head> <body> 

So finally, in your.js you can use them as usual:

 alert(helloMessage); console.log("Config is..."); console.log(myJsConfig); 
+4
source

The error is actually not in the isEmpty() .

If you look at the Play Console, the compiler output will point to another place:

 ! @6e12h60d9 - Internal server error, for (GET) [/] -> sbt.PlayExceptions$TemplateCompilationException: Compilation error[Not parsed?] at sbt.PlayCommands$$anonfun$43.apply(PlayCommands.scala:433) ~[na:na] at sbt.PlayCommands$$anonfun$43.apply(PlayCommands.scala:409) ~[na:na] at sbt.Scoped$$anonfun$hf5$1.apply(Structure.scala:581) ~[na:na] at sbt.Scoped$$anonfun$hf5$1.apply(Structure.scala:581) ~[na:na] at scala.Function1$$anonfun$compose$1.apply(Function1.scala:49) ~[scala-library.jar:na] at sbt.Scoped$Reduced$$anonfun$combine$1$$anonfun$apply$12.apply(Structure.scala:311) ~[na:na] [warn] play - No application found at invoker init [error] C:\Users\maba\Development\play\layout\app\views\main.scala.html:14: Compilation error[Not parsed?] [error] //for(var prop in obj) { [error] ^ [error] (compile:sources) @6e12hd81d: Compilation error in C:\Users\maba\Development\play\layout\app\views\main.scala.html:14 [error] application - 

Thus, it seems that the error processes the comments (I don’t know why at the moment).

So, if you want this code to run, use Play comments ( @* some comment *@ ) instead:

 <script> function isEmpty(obj) { @* for(var prop in obj) { if(obj.hasOwnProperty(prop)) *@ try{ if(JSON.stringify(obj)=='{}'){ return true; }else{ return false; } }catch(e){ return false; } } </script> 
+4
source
  function isEmpty(obj) { try{ if(JSON.stringify(obj)=='{}'){ return true; }else{ return false; } }catch(e){ return false; } 

}

0
source

Like @biesior described in the second point, @Html() outputs the code (html / js) from the controller. But we can compile it without passing it as a parameter:

 @Html(new String( """ <script> </script> """) ) 
0
source

All Articles