This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Pages: 1 - Last Post: 3 Nov 22, 13:32 Last Post By: AndreWolff
AndreWolff

Posts: 1,116
Registered: 14-Dec-2007
How should I use user variables in Groovy?
Posted: 3 Nov 22, 11:56
 
  Click to reply to this thread Reply
I use a user variable audioHint to define a hint to be displayed if you hover the mouse over an audio control.

To handle that, I use in Beanshell next code in index.htt:
<%
trackHint="";
audioStart = "<audio controls  ";
if (autoPlay) audioStart = "<audio controls autoplay ";
if ((audioHint != void)&& (audioHint != null)) trackHint=audioHint;
trackHint= trackHint.replaceAll("'","'").replaceAll("\"",""");
if ((audioClipPath != void)&& (audioClipPath != null)) {
  sAudio=true;
  out.print("data-audio=\"true\" ");
}
 %>
This is not accepted in Groovy, so I tried this with debug statements:
<%
trackHint="";
audioStart = "<audio controls  ";
if (autoPlay) audioStart = "<audio controls autoplay ";
System.out.println("1 audioHint: " + audioHint); 
if (!isEmpty(audioHint)) {
//trackHint=audioHint;
System.out.println("2 audioHint: " + audioHint); 
}
//if ((audioHint != void)&& (audioHint != null)) trackHint=audioHint;
trackHint= trackHint.replaceAll("'","'").replaceAll("\"",""");
if (!isEmpty(audioClipPath)) {
//if ((audioClipPath != void)&& (audioClipPath != null)) {
  sAudio=true;
  out.print("data-audio=\"true\" ");
}
 %>
I get with this code this error:
Making "FB_JustifiedGallery_Simple_groovy" (Changes)
se.datadosen.util.ScriptException: groovy.lang.MissingPropertyException: No such property: audioHint for class: Script3147 in index.htt at line number 529
	at se.datadosen.util.ScriptException.of(ScriptException.java:59)
	at se.datadosen.jalbum.ast.ScriptletNode.doEval(ScriptletNode.java:99)

So how should I use / declare a user variable in Groovy?
davidekholm

Posts: 3,996
Registered: 18-Oct-2002
Re: How should I use user variables in Groovy?
Posted: 3 Nov 22, 12:35   in response to: AndreWolff in response to: AndreWolff
 
  Click to reply to this thread Reply
In Groovy, you can use
if (binding.hasVariable("foo")) { ... } 
to test for variable existence.
AndreWolff

Posts: 1,116
Registered: 14-Dec-2007
Re: How should I use user variables in Groovy?
Posted: 3 Nov 22, 13:32   in response to: davidekholm in response to: davidekholm
 
  Click to reply to this thread Reply
davidekholm wrote:
In Groovy, you can use
if (binding.hasVariable("foo")) { ... } 
to test for variable existence.

In Beanshell I used this function:
boolean isEmpty(Object o) {
	return o == void || o == null || o.toString().trim().equals("");
}
to check if a variable exists and has some value, but that is apparently not allowed in Groovy.

I replaced the code by
<%
trackHint="";
audioStart = "<audio controls  ";
if (autoPlay) audioStart = "<audio controls autoplay ";
if (exists.audioHint) trackHint=audioHint;
trackHint= trackHint.replaceAll("'","'").replaceAll("\"",""");
if (exists.audioClipPath) {
  sAudio=true;
  out.print("data-audio=\"true\" ");
}
 %>
and now it is accepted by Groovy.

Thanks for your help!
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums