【问题标题】:Grails custom tag library out multiple linesGrails自定义标签库出多行
【发布时间】:2015-01-28 11:45:07
【问题描述】:

我正在制作一个自定义标签库,我必须在其中使用标签库将 html 内容输出到视图。我们可以输出如下内容-

def globalCSS = { attrs, body ->
    out << '<!-- BEGIN GLOBAL MANDATORY STYLES -->'
    out << '<link href="http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700&subset=all" rel="stylesheet" type="text/css">'
    out << '<link rel="stylesheet" type="text/css" href="'+g.resource(dir: 'assets/global/plugins/font-awesome/css', file: 'font-awesome.min.css', absolute: true)+'"/>'
    out << '<link rel="stylesheet" type="text/css" href="'+g.resource(dir: 'assets/global/plugins/simple-line-icons', file: 'simple-line-icons.min.css', absolute: true)+'"/>'
    out << '<link rel="stylesheet" type="text/css" href="'+g.resource(dir: 'assets/global/plugins/bootstrap/css', file: 'bootstrap.min.css', absolute: true)+'"/>'
    out << '<link rel="stylesheet" type="text/css" href="'+g.resource(dir: 'assets/global/plugins/uniform/css', file: 'uniform.default.css', absolute: true)+'"/>'
    out << '<!-- END GLOBAL MANDATORY STYLES -->'
}

现在的问题是,如果我尝试在一个out 中输入多行,那么它会引发一个错误-

def globalCSS = { attrs, body ->
    out << 'Bla bla bla
    Super bla bla
    Awesome bla bla'
}

错误-

Multiple markers at this line
- implements groovy.lang.Script.run
- Groovy:expecting ''', found '\r' @ line 10, 

那么,有没有办法将多行一并发布?因为我很容易复制粘贴我需要输出的 HTML 代码,所以请提出一种方法。

【问题讨论】:

    标签: taglib


    【解决方案1】:

    请参阅http://groovy.codehaus.org/Strings+and+GString 了解Multiline Strings 部分。使用'''""" 在groovy 中环绕多行字符串。

    如果您希望使用一个文本块,但又不想对其全部进行编码(例如,如果它是一个 HTML 块或其他内容),那么您可以使用 """ 语法。

     def text = """\
     hello there ${name}
     how are you today?
     """
    

    第一行末尾的\ 表示应该忽略那里的换行符(为了便于阅读)

    【讨论】:

    • 好极了。谢谢它简单而甜蜜。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多