繁体   English   中英

Grails:资产管道和GSP模板

[英]Grails: Asset Pipeline and GSP Templates

我已经从使用Resources插件切换到新的Asset Pipeline插件。 但是,我遇到了一个我不确定如何修复的问题。

我使用了几个模板(即:_template.gsp),这些模板通过其他GSP文件中的g:render标签包含在内。

_template.gsp:

<%@ page contentType="text/html;charset=UTF-8" %>
<asset:stylesheet src="_template.css"/>
<asset:javascript src="_template.js"/>
<div>
...
</div>

其他GSP文件:

...
<g:render template="/template"/>
...

在我的_template.gsp文件中,我包含了模板中的代码工作和/或看起来正确所需的几个资产。 当我使用资源插件来实现这一点时,事情按预期工作。 模板中包含的任何文件都被移动到生成的GSP文件的HEAD部分。 但是,使用Asset Pipeline插件,它们会保留在调用GSP文件中包含模板的位置。 更糟糕的是,它们未正确处理,因此未在生成的HTML文件中正确加载它们。

例如,在调试中,生成的HTML文件如下所示

...
<link rel="stylesheet" href="/assets/_template.css?compile=false"/>
<script src="/assets/_template.js?compile=false" type="text/javascript"></script>
<div>
...
</div>
...

并且一切正常(尽管理想情况下该文件应该像在使用Resources插件时一样加载到HEAD部分)。

在生产中,生成的HTML文件如下所示:

...
<link rel="stylesheet" href="/assets/_template.css"/>
<script src="/assets/_template.js" type="text/javascript"></script>
<div>
...
</div>
...

但是,在生产中,所有其他包含的资产(实际GSP文件中包含的文件)具有更长的文件名,看起来像样式-aa85c6fa983d13b6f58e12b475e9d35c.css。 模板中的_template.css和_template.js文件未转换为其中一个长文件名,如果我尝试访问/assets/styles.css路径,我只会得到一个空白页面。

通过创建以下标记库,我能够解决问题的第一部分(资产不在HEAD中):

class TemplateAssetsTagLib
{
  // Define the namespace and encoding
  static namespace = 'tasset'
  static defaultEncodeAs = 'raw'

  // Tag called to move the content of this tag to where the assets tag is located (usually the HTML HEAD section)
  def head = { attrs, body ->
    // Get any existing asset blocks
    def assetBlocks = request.getAttribute('templateAssetBlocks')
    if(!assetBlocks)
      assetBlocks = []

    // Add the body of this tag to the asset blocks list
    assetBlocks << body()
    request.setAttribute('templateAssetBlocks', assetBlocks)
  }

  // Tag called to load any content that was saved using the head tag
  def assets = { attrs ->
    // Get all existing asset blocks
    def assetBlocks = request.getAttribute('templateAssetBlocks')
    if(!assetBlocks)
      return

    // Output the asset blocks
    assetBlocks.each { assetBlock ->
      out << assetBlock
    }
  }
}

它在Asset Pipeline延迟脚本功能之后被镜像,但更通用。

而且我已经能够通过简单地重命名资产和删除前导下划线来解决第二个问题。 出于某种原因,具有前导下划线的资产在WAR创建期间不会被编译,因此在生产模式下无法访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM