Code reuse in templates

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Code reuse in templates

rahulsom
Is it possible to use reusable code in any of the templating choices?

I'm looking for something like taglibs or freemarker functions.

I tried this in my ratpack.groovy:

                get('') {
                        def map = [
                                        formField: {cssClass, label, id, jsonLocation ->
                                                def newMap = [
                                                        cssClass: cssClass, label: label, id:id, jsonLocation:jsonLocation
                                                ]
                                                render groovyTemplate(newMap, 'formField.html')
                                        }
                        ]
                        render groovyTemplate(map, "index.html")
                }


Then I defined a formField.html that uses these fields, and tried calling it in my index.html

I got this in my stacktrace:

Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
index.html: 103: [Static type checking] - Cannot find matching method ratpack.groovy.templating.TemplateModel#formField(java.lang.String, java.lang.String, java.lang.String, java.lang.String). Please check if the declared type is right and if the method exists.
 @ line 103, column 2.
    model.formField('col-md-6', 'First Name', 'firstName', 'patient.firstName')
    ^

1 error
Reply | Threaded
Open this post in threaded view
|

Re: Code reuse in templates

uris77
In your html, it should be $model.formField or  ${html model.formField}
Reply | Threaded
Open this post in threaded view
|

Re: Code reuse in templates

danhyun
In reply to this post by rahulsom
Hi rahulsom,

Ratpack actually provides many flavors of templating. There's Groovy Templates, Thymeleaf, Handlebars and Groovy Markup Template Engine.

In the example-books project, we are using the Groovy Markup Template Engine.

There's a file called _book_form.gtpl which is used from create.gtpl and update.gtpl.

Hope this helps.
Reply | Threaded
Open this post in threaded view
|

Re: Code reuse in templates

rahulsom
In reply to this post by uris77
In reply to uris77, I am doing this:

    ${model.formField('col-md-6', 'First Name', 'firstName', 'patient.firstName')}

Thanks.
Reply | Threaded
Open this post in threaded view
|

Re: Code reuse in templates

rahulsom
In reply to this post by danhyun
@danhyun,

Thanks. The problem with groovyMarkupTemplate is you can't develop html in isolation. I'll see if one of the other options fits my needs better.
Reply | Threaded
Open this post in threaded view
|

Re: Code reuse in templates

Luke Daley
Administrator
In reply to this post by rahulsom
This is a problem with the Groovy static compiler. You can either turn static compilation off for templates, or cast the `formFields` model value.

<code>
def formField = model.formField as Closure

formField(…)
</code>

As Danny points out, there are other options and all of them have more features than the `groovyTemplate` option.