简体   繁体   中英

CMS inside a grails app

i'm trying to build a very simple CMS functionality within a grails app. Basically what I'm trying here is having a gsp in a string and I would like to build the gsp from this string.

It works, but the resulting page is not decorated with the main layout. Furthermore in some other tries I noticed that model passed to the make method is ignored.

I'm using grails 2.0.1.

Thanks

class HomeController {

    def groovyPagesTemplateEngine

    def cms() {
        def page = """<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="layout" content="main"/>
<title>CMS page using your application's main layout</title>
</head>
<body>
CMS page using your application's "main" layout.
</body>
</html>"""

        StringWriter sw = new StringWriter()
        PrintWriter pw = new PrintWriter(sw)

        groovyPagesTemplateEngine.createTemplate(page, "somepage.gsp").make([nome: "ciao"]).writeTo(pw)
        render sw
    }

That doesn't work because you're essentially just calling render with a string.

The layout gets applied when the sitemesh filter gets called. In order to get this to work, you would have to get the sitemesh decorators applied to your string.

When we were building a similar feature, all we did was to send the relevant page fragments:

ie,

Mytemplate.gsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="layout" content="${layout}"/>
${head}
</head>
<body>
${body}
</body>
</html>

and then you can just call:

render( template: 'MyTemplate', model:[ layout: 'layout', head: '...', body: '...' ] )

This has the same effect of what you're trying to do but with the addition of sitemesh ( and potentially plugins integrating correctly ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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