我正在尝试使用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")}'
]
]" />
这不起作用,我确定我的语法在某处错了。你能在g里面定义一条'$resource(dir)'
路径:每个路径都像我一样吗?也许我需要使用g:link
?这可以用Grails完成吗?
答案 0 :(得分:2)
听起来你只需要使用资源标签。在ApplicationResources.groovy
中定义您的“资源”。然后,在您的布局中包含r:layoutResources
标记,最后,在gsp中指定要包含在该页面上的资源模块。
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'
}
}
<head>
...
<r:layoutResources />
</head>
<body>
...
<r:layoutResources />
</body>
<head>
...
<r:require modules="application" />
</head>
<body>
...
</body>
<head>
...
<r:require modules="reports" />
</head>
<body>
...
</body>
<head>
...
<r:require modules="charting" />
</head>
<body>
...
</body>