Search code examples
grails

grails 4 call a namespaced taglib from another taglib


How can I access a taglib wwith a different namespace from another taglib in grails 4? I can't seem to cross call.

Here's an example

 class MyLibTagLib{

  static namespace="myLib"

  def fancy={attrs->
      ....
  }
}

import app.lib.MyLibTagLib
class CallerTagLib{

  //in the default 'g' namespace

  //def myLib //injection fails

  def fancyText={attrs->
      //fails
      out << myLib.fancy(attrs)
      //also fails
      grailsApplication.mainContext.getBean('app.lib.MyLibTagLib')
  }
}

Solution

  • I cannot reproduce the problematic behavior that you describe. See the project at https://github.com/jeffbrown/tagquestion.

    https://github.com/jeffbrown/tagquestion/blob/9a389f957c8327d071e55a131b9c8a83663a6f3e/grails-app/taglib/demo/MyLibTagLib.groovy

    package demo
    
    class MyLibTagLib {
        static namespace="myLib"
    
        def fancy={attrs->
            out << 'Something Fancy'
        }
    }
    

    https://github.com/jeffbrown/tagquestion/blob/9a389f957c8327d071e55a131b9c8a83663a6f3e/grails-app/taglib/demo/CallerTagLib.groovy

    package demo
    
    class CallerTagLib {
    
        def fancyText = { attrs ->
            out << myLib.fancy(attrs)
        }
    }
    

    The main index.gsp at https://github.com/jeffbrown/tagquestion/blob/9a389f957c8327d071e55a131b9c8a83663a6f3e/grails-app/views/index.gsp#L56 invokes <g:fancyText/> and that works.