在 Play Framework 中,它通常有 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 这样的 View 时,它通常以
开始#{extends 'main.html' /}
并且 index.html 只包含正文部分( #{doLayout/>
in main.html )。
我想根据加载的页面添加 javascript(例如,在页面 coffee.html 中我想加载 water.js)。现在我必须将它添加到 main.html 中并在整个应用程序中加载它。
1/如何从扩展 View (index.html)中加载?
2/此外,如何获取 baseUrl 值(如 Zend 框架)以传递给 javascript 变量?
最佳答案
您可以使用 set 和 get 标签来做到这一点。
如果你的main.html是这样的
<title>#{get 'title' /}</title>
#{get 'moreScripts' /}
然后在你不同的 View 中你可以设置不同的JS。比如index.html可能没有JS,但是coffee.html会有
#{set 'moreScripts'}
<script src="@{'/public/javascripts/yourscript.js'}" type="text/javascript" charset="utf-8"></script>
#{/set}
关于javascript - Playframework - 在 main.html 之外附加 JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6260424/