0

I found a code snippet that looks like this

createReportProfile = function () {

var url = "<%= createLink(controller: 'configCashFlowReport', action: 'saveCreate')%>";
var data = $("form[id=addReportConfigForm]").serializeArray();
var doneFunction = function (data) {
    $("#returnAddReportProfiletDiv").html(data);
}

ajaxGet(url, data, doneFunction, undefined);

return false; }

Please take note of this line

var url = "<%= createLink(controller: 'configCashFlowReport', action: 'saveCreate')%>";

CreateReportProfile function is inside script tags and is saved in report.gsp. Obviously, the code is written using javascript but there is a grails code inside it (createLink).

1) How was grails able to detect that it should evaluate the createLInk tag? Because of <%= %> ?? But it is inside a double quote. How does grails differentiate a simple string and a string that should be evaluated?

2) How is .gsp file parsed?

user3714598
  • 1,733
  • 5
  • 28
  • 45
  • 1
    Take a look here http://stackoverflow.com/a/2314695/755637. You can set grails.views.gsp.keepgenerateddir='/some/existing/directory' which when your app compiles gsps - will dump the compiled code there for you to review. Good for debugging. The reason for such a thing may be around codecs. Take a look here to understand it better. https://jira.grails.org/browse/GPCKEDITOR-40?focusedCommentId=81082&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-81082 – V H Dec 22 '15 at 16:29

1 Answers1

2

Why you think such code is something hard to parse? For GSP it doesn't matter what content it have, html, js, or css, or anything else. All of this is text. There are no semantics associated with it, besides GSP tags/blocks itself. Everything else outside such blocks are characters passed as is.

You can use <g:createLink /> syntax instead, or ${createLink}. I think <%= createLink %> was used there just because it's easier to read in Javascript code, and most IDEs will highlight it correctly w/o any extra effort.

Igor Artamonov
  • 35,450
  • 10
  • 82
  • 113