gpt4 book ai didi

html - 使用 Groovy/Grails 以编程方式使用 html 模板

转载 作者:行者123 更新时间:2023-11-27 22:36:58 27 4
gpt4 key购买 nike

我正在尝试使用带有 Grails 应用程序的 html 模板。我得到了一个 URL,需要在运行时将 html 动态加载到我的 Grails View 中。在 HTML 代码中有一个 {title} 和一个 {content} 标签,我的 Grails 代码将被注入(inject)其中。

在 PHP 中,它类似于 include("url");现在我们如何在 Grails 中做到这一点,或者这可能吗?

最佳答案

没有缓存也没有真正诱人的独立,你的代码可能看起来像这样:

def template = new Url('http://example.com').getText()
def html
html = html.replaceAll('{title}','my Title')
html = html.replaceAll('{content}','my Content')

但是 Raphael 是绝对正确的:如果您需要更复杂的解决方案 (http://groovy.codehaus.org/Groovy+Templates),您应该看看 groovy 模板框架。

使用模板框架,你首先要准备好你的模板

import groovy.text.SimpleTemplateEngine
def template = new Url('http://example.com').getText()
template = template.replaceAll('{title}','${title}')
template = tamplate.replaceAll('{content}','${content}')

并将其作为缓存保存到数据库中。当您必须修改 HTML 页面时,您可以获取模板并让 groovy 替换占位符:

def binding = [title:"my Title", content:"my Content"]
def engine = new SimpleTemplateEngine()
html = engine.createTemplate(template).make(binding)

就是这样。

关于html - 使用 Groovy/Grails 以编程方式使用 html 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13370950/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com