-1

grails 3.3.9, + tag libs

I create a new taglib like this

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class JavaDateTimeTagLib {
    static defaultEncodeAs = [taglib:'html']
    //static encodeAsForTags = [tagName: [taglib:'html'], otherTagName: [taglib:'none']]
    static encodeAsForTags = [testCall: [taglib:'none']]

    static namespace = "jdt"        //java8 date time name space for tags

    def testCall = { attrs ->

        def p1 = attrs.p1
        def p2 = attrs.p2
        out << "p1:'$p1' with class ${p1.getClass()}"
        out << "p2:'$p2' with class ${p2.getClass()}"
    }
}

where i want to pass a non string variable to the attrs map.

i then setup the test like this

class JavaDateTimeTagLibSpec extends Specification implements TagLibUnitTest<JavaDateTimeTagLib> {

    def setup() {
    }

    def cleanup() {
    }

    /**
     * restriction params must be quoted values - esentially strings
     * taglib has to take that and do any conversions in the taglib
     * output by defaults is encoded html using std codec
     */
    void "call displayDateTime tag "() {
        given:

        String result = applyTemplate('<jdt:testCall p1="p1-string" p2="$now"  />' , [now:LocalDateTime.now()])

        when :
        println "$result "

        then:
        result
    }
}

What i'm trying to do is pass a LocalDateTime variable to the attrs map.

if you use applyTemplate and pass p2=ldt, and test map [ldt:LocalDateTime.now()] the test fails saying the variable must be 'quoted

[Byte array resource [test_1549319950845]:1] Attribute value must be quoted (p1="p1-string" p2=now).

if you quote the p2 variable using p2="$ldt" and test map the sname as [ldt:LocalDateTime.now()], then the test will work - However the type passed to the attrs map is GStringImpl

however reading the OCI guide oci tag lib guide

it implies on page 4 that you can pass attrs.employees as a list of domainobjects and use that in you markup

but theres no way to invoke this using the testing as everything has to be string quoted - which makes it GStringImpl

how do you pass non string variable to a taglibs attrs map from appyTemplate (.. i presume the same restiction applies in live gsp and not just the testing framework )

WILLIAM WOODMAN
  • 1,185
  • 5
  • 19
  • 36

1 Answers1

0

hah! its such a small thing

if you change the test and write this

    String result = applyTemplate('<jdt:testCall p1="p1-string" p2="${now}"  />' , [now:LocalDateTime.now()])

with braces round the variable key name from the map - then the actual value of the typed variable is passed into the taglib. so if you look in the attrs.p2 value it has the actual LocalDateTime instance.

now quite sure how applytemplate is working not sure, but expecting its trying to do soemthing like groovy shell evaluate the string - hence the need to use single '' like '

Hope that makes sense

blimey enough to make your head explode sometimes

WILLIAM WOODMAN
  • 1,185
  • 5
  • 19
  • 36