Playframework-在main.html外部附加JS

Playframework - Append JS outside the main.html

本文关键字:JS 外部 html main Playframework-      更新时间:2023-09-26

在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这样的视图时,它通常以开始

#{extends 'main.html' /}

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

我想根据加载的页面添加javascript(例如,在pagecoffee.html中,我想加载water.js)。现在我必须将它添加到main.html中,并在整个应用程序中加载它。

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

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

您可以使用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}