GSP tags in JavaScript

Dónal picture Dónal · Sep 7, 2011 · Viewed 7.1k times · Source

I had the following in the <head> of a GSP

<script type="text/javascript>
    $("button.remove-item").click(function() {
        $.ajax({
            url: "${createLink(action: 'remove', controller: 'cart')}",
            type: 'POST'
        });
    });
</script>

Notice that I'm using the Grails createLink tag to construct the URL that the AJAX request will post to. When I moved this code into checkout.js and replaced the block of code above with:

<script type="text/javascript" src="${resource(dir: 'js', file: 'checkout.js')}"></script>

the createLink tag is no longer evaluated by Grails. So it seems that Grails tags within <script> blocks are evaluated, but tags within .js files included by GSPs are not - is there a way to change this?

Answer

schmolly159 picture schmolly159 · Sep 7, 2011

Check out the GSParse plugin to have css and js parsed as a gsp file:

http://nerderg.com/GSParse

http://grails.org/plugin/gsp-arse