0

我正在尝试使用 Grails 应用程序模仿 Django 中的模板继承。我希望能够定义一个“_header.gsp”,其中包括应用程序中的所有共享资源:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>${title}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    %{--Shared Styles--}%
    <link rel="stylesheet" href="${resource(dir: 'app/shared/css/bootstrap', file: 'bootstrap.min.css')}" type="text/css">

    %{--Shared Libraries--}%
    <script src="${resource(dir: 'lib/jquery', file: 'jquery.js')}"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>

    %{--View-specific styles--}%
    <g:each var="style" in="${styles}">
        <link rel="stylesheet" href="${style}" type="text/css">
    </g:each>

    %{--View-specific scripts--}%
    <g:each var="include" in="${includes}">
        <script src="${include}" type="text/javascript"></script>
    </g:each>

对于每个特定的视图模板,我将在 _header.gsp 中包含一个字典,以填写特定于视图的要求:

<g:render template="/header"
          model="[
            title:'Alerts',
            styles:[
                    '${resource(dir: "app/stuff/css", file: "other.css")}',
                    '${resource(dir: "app/stuff/css", file: "second.css")}'
                    ],
            includes:[
                    '${resource(dir: "app/stuff/src/main/js/", file: "app.js")}',
                    '${resource(dir: "app/stuff/src/main/js/", file: "filters.js")}'
                    ]
            ]" />

这不起作用,我确定我的语法在某处是错误的。你能'$resource(dir)'像我一样在 ag:each 中定义一条路径吗?也许我需要使用g:link?这可以用 Grails 完成吗?

4

1 回答 1

2

听起来您只需要使用资源标签。在 中定义您的“资源” ApplicationResources.groovy。然后,在您的布局中包含r:layoutResources标签,最后,在 gsp 中指定要包含在该页面上的资源模块。

在 ApplicationResources.groovy

modules = {

    application {
        dependsOn 'jquery'

        resource url: 'css/other.css'
        resource url: 'css/second.css'
        resource url: 'js/bootstrap.js'
    }

    charting {
        //Charting is dependent on the 'application' resource module above, 
        // so it will include everything from the application and the 
        // charting css and js.
        dependsOn 'application'  

        resource url: 'css/chart.css'
        resource url: 'js/pie-chart.js'
    }

    reports {
        //Completely separate so there is no dependsOn.  
        // Like 'application' module, it will only include the resources below.

        resource url: 'css/pdf.css'
        resource url: 'js/interactive-report.js'
    }
}

在 /grails-app/layouts/main.gsp

<head>
  ...
  <r:layoutResources />
</head>
<body>
  ...
  <r:layoutResources />
</body>

在 /grails-app/views/someDomain/

列表.gsp

<head>
  ...
  <r:require modules="application" />
</head>
<body>
  ...

</body>

报告.gsp

<head>
  ...
  <r:require modules="reports" />
</head>
<body>
  ...
</body>

图表.gsp

<head>
  ...
  <r:require modules="charting" />
</head>
<body>
  ...
</body>
于 2013-09-18T20:43:46.137 回答