2011-06-07 50 views
1

在游戏框架,它通常有main.html中与像代码之外:Playframework - 追加JS的main.html中

<html> 
    <head> 
     <title>#{get 'title' /}</title>  
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
     <link rel="stylesheet" type="text/css" media="screen" 
      href="@{'/public/stylesheets/main.css'}" /> 
     <link rel="shortcut icon" type="image/png" 
      href="@{'/public/images/favicon.png'}" /> 
    </head> 
    <body> 
     #{doLayout /} 
    </body> 
</html> 

所以当我有一个观点,如index.html的,它通常与

启动
#{extends 'main.html' /} 

而index.html只包含正文部分(main.html中的#{doLayout /})。

我想添加javascript取决于页面加载(在页面coffee.html我想加载water.js例如)。现在,我必须将其添加到main.html中,并将其加载到应用程序中。

1 /它如何从扩展视图(index.html)加载?

2 /另外,如何获得baseUrl值(如Zend框架)传递给javascript变量?

回答

2

你可以用set和get标签来做到这一点。

如果你的main.html中是这样

<title>#{get 'title' /}</title>  
#{get 'moreScripts' /} 

然后在你的不同意见,你可以设置不同的JS。例如,index.html的可能没有JS,但coffee.html将具有

#{set 'moreScripts'} 
    <script src="@{'/public/javascripts/yourscript.js'}" type="text/javascript" charset="utf-8"></script> 
#{/set} 
+0

我只是想,发现 “/” 必须消除:#{集 'moreScripts'} #{/ set} – 2011-11-10 15:47:20

+0

是的,您是对的。斜线会关闭标签,并忽略内容。我已经更新了答案以显示更正。 – Codemwnci 2011-11-10 17:20:06

+0

那么通过将脚本标签包装在“morescripts”标签中可以获得什么优势? 无论如何,为什么不在咖啡html中包含脚本,就像使用“普通”html页面一样? – 2013-02-28 09:16:08