Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:42:59+00:00 2026-06-17T11:42:59+00:00

I’m trying to write a Grails custom tag that (among other things) triggers inclusion

  • 0

I’m trying to write a Grails custom tag that (among other things) triggers inclusion of a resource, so something like <myTags:view name="foo"/> would load, say, js/views/foo.js. And I want it loaded with disposition: 'head'.

I could use <r:external/>, but that wouldn’t put it in the <head>, it would just produce an inline <script/> tag. And I could use <r.script/>, but that doesn’t let me reference a path; I’d have to have my custom tag read the file and dump it to out.

Now, if foo.js was its own module, I could do something like: r.require([module: 'foo']), but it’s not; part of the point of this is that I don’t want to have to declare all of these files in ApplicationResources.groovy. But maybe I could have ApplicationResources.groovy create the modules programmatically, by reading through the available files — is that possible? Or is there a better way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:43:01+00:00Added an answer on June 17, 2026 at 11:43 am

    I ended up going in the direction of having ApplicationResources.groovy create modules programmatically, so the custom tag can use <r:require/>.

    The idea is, for each Backbone view, under web-app/myApp/views, there’s a Backbone view in a .js file, and a Handlebars template in a .handlebars file (with the same name, by convention). The .handlebars file gets declared as an ordinary module, but gets precompiled by the Handlebars-Resources plugin.

    Some code in ApplicationResources.groovy finds all the views and creates corresponding resource modules:

    GrailsApplication grailsApplication = Holders.getGrailsApplication()
    File viewsDir = grailsApplication.parentContext.getResource("myApp/views").file;
    if (viewsDir.exists() && viewsDir.isDirectory() && viewsDir.canRead()) {
        String[] viewsJS = viewsDir.list().findAll { name -> 
            name.endsWith("View.js") 
        }
        String[] views = viewsJS.collect { name ->
            name.substring(0, name.length() - ".js".length())
        }
    
        for (view in views) {
            "${view}" {
                dependsOn 'backbone', 'backbone_relational', 'handlebars'
                resource url: "dpg/views/${view}.handlebars", 
                         attrs: [type: 'js'], 
                         disposition: 'head'
                resource url: "dpg/views/${view}.js", 
                         disposition: 'head'
    
            }
        }
    }
    

    Then the taglib:

    class ViewsTagLib {
        static namespace = "myApp"
    
        def view = { attrs ->
            r.require(module: "${attrs.name}View")
            out << "<${attrs.tagName} id='${attrs.id}'></${attrs.tagName}>"
        }
    }
    

    Invoking it in a GSP:

    <myApp:view tagName="div" name="foo" id="foo1"/>
    <myApp:view tagName="div" name="foo" id="foo2"/>
    

    Produces:

    <html>
        <head>
            ...
            <!--
                Automagically generated modules (included only once).
                Should put these in the same bundle, but handlebars-resources
                gets confused.
            -->
            <script src="/myApp/static/bundle-bundle_fooView_handlebars.js" 
                    type="text/javascript" ></script>
            <script src="/myApp/static/bundle-bundle_fooView_head.js" 
                    type="text/javascript" ></script>
        </head>
        <body>
            ...
            <div id="foo1"></div> <!-- backbone placeholder for 1st view instance-->
            <div id="foo2"></div> <!-- backbone placeholder for 2nd view instance-->
        </body>
    </html>
    

    It’s not pretty but the mess is mostly hidden, and it should cut down considerably on boilerplate and on opportunities to forget to add magic strings to multiple files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.