Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] - ability to use JavaExpressions in String literale like GString #19

Closed
tunggad opened this issue Jun 15, 2011 · 26 comments

Comments

@tunggad
Copy link

tunggad commented Jun 15, 2011

`String value = "something";
`String anotherValue = "Hello ${something}, im a another value";

Handy to use and save us alot of string-concats and the view codes look then abit like "view code" rather than coding view in Servlet :-D, if u know what i mean.

PS: pls also note the ability to escape the tow symbols $ and ~ .

@branaway
Copy link
Owner

are you asking to create a another template mechanism in Japid? not sure about that.

Escaping: I'll have something for that. In the meantime you can always do ${''}, ${'$'}, or sometimes $'', $'$', or using the html entity code: &xxx;

@branaway
Copy link
Owner

version 0.8.6 added support for using ~ as the generic escape marker: ~~, ~@, ~#, ~$, etc..

@tunggad
Copy link
Author

tunggad commented Jun 16, 2011

Why another template mechanism? I dont understand you. I just want this following:
when we in script mode, we can do: `String value = "Hello ${javaEpression}, im a String";. And in generated code we will have String value = "Hello " + javaExpression + ", im a String";

Just a handy way to build up String values in script mode without string-concats. Im porting a Grails app -> Japid + Play!, and my Grails tags ultilize this syntax that much.

@branaway
Copy link
Owner

Yea I know that. It's called GString, which internally is a small template engine.

@tunggad
Copy link
Author

tunggad commented Jun 16, 2011

Exactly, GString. But do you think, is it not easy to implement this feature or somehow to mimic this behavior in Japid?

I think, it's one of the most important syntax sugar to avoid the feeling "we are coding views in Java".

@branaway
Copy link
Owner

But I don't want to let people feel they're coding in yet another language. I got a a few comments saying using pure Java is an advantage. String.format() in the interim?

@tunggad
Copy link
Author

tunggad commented Jun 16, 2011

Ok, String.format() + multi-line continuer is good enough. I have totally forgot it. Thank you!

@tunggad tunggad closed this as completed Jun 16, 2011
@tunggad
Copy link
Author

tunggad commented Jun 20, 2011

Hi Bing,

After days working with Japid, i feel, im really missing the ability to build simple String template inline, like Goovy GString. All the time i need to use String.format(). And using String.format doesnt seems to help much, the code is still not reader-friendly (ok, abit better than String-concat) and performance of String.format is worse. Because everytime String.format is called, it must be going to create a Fomatter instance first.

I think, using of mass String.format could hurt performance and violates Japid's philosophy (raw performance is the King) and Japid's image too. And i think, other users would also like ithis feature, especially if they coming from dynamic script language background.

`String snip = "Hello ${user.usename}!, you have a message from ${message.sender.username}"

==>
String snip = "Hello " +user.username+ "!, you have a message from "+message.sender.username+

With this features my code would be much cleaner and performanter. Pls target it in your plan Bing!

@tunggad tunggad reopened this Jun 20, 2011
@branaway
Copy link
Owner

Let me think about it, OK?

@branaway
Copy link
Owner

Can you use local methods to get the same effect?

` String a = "Somthing";
` String b = "hello ${a}!"

would become:

`def hi(String s) 
      hello $s!
`
` String a = "something";
` String b = hi(a);

So basically extract all string interpolation logic into local methods and invoke them in your main logic block.

Local methods are mini templates.

@tunggad
Copy link
Author

tunggad commented Jun 20, 2011

ok, with local methods i will not have performance problem as with String.format. But i think, the two other problems are still there:

  • usage is not handy, for your small example above, from 2 lines of code we have now 5
  • code is not reader-freindly, main ui logic gets scattered with small local methods, difficult to read & follow.

I think, local methods only makes sense when we ever want to reuse them multiple times.

@branaway
Copy link
Owner

OK, version 0.8.8.2 has been specifically built for you. It's really a hack , but it will pre-process the source code and expand anything of pattern $[xxx] to " + xxx + ". It's a pre-processor and does not have any intelligence in parsing the code.

@tunggad
Copy link
Author

tunggad commented Jun 20, 2011

oh yeah, cool! I was also talking about such a pre-processing of source code, one dont need here some powerful, automagical feature which hurts performance. i will try it right now.

@tunggad
Copy link
Author

tunggad commented Jun 20, 2011

      `callbackSnip = "function htmlSuccess_$[id] (responseText, statusText, xhr, jqForm) {\n\
                            var data = responseText;\n\
                            $[reRenderSnip(reRender, dataType)]\n\
                            $[success]\n\
                        }";`

----> generated code:

callbackSnip = "function htmlSuccess_"+id+" (responseText, statusText, xhr, jqForm) {\n var data = responseText;\n $[reRenderSnip(reRender, dataType)]\n "+success+"\n }";// line 30

$[reRenderSnip(reRender, dataType)] was not pre-processed.

@tunggad
Copy link
Author

tunggad commented Jun 20, 2011

`String[] testArray = {...};`
`String value = "Hello $[testArray[0]]";`

$[testArray[0]] will also not be pre-processed.

@branaway
Copy link
Owner

Can you try the refreshed 0.8.8.2? I was too restrict in pattern

@tunggad
Copy link
Author

tunggad commented Jun 20, 2011

i could not force Play! to install the new refreshed 0.8.8.2. I have tried:

  • back to 0.8.8.1 (here the old version was downloaded newly)
  • 0.8.8.2 again, but no new download was issued. --> no changes

@branaway
Copy link
Owner

remove the japid directory in the modules directory and try again. Depending on how you have specified the dependencies. it's either in the play's modules directory or in the modules directory of your application.

@tunggad
Copy link
Author

tunggad commented Jun 20, 2011

i also tried both before, no success. At my Play! modules folder there is no Japid-module folder, only in project.

@tunggad
Copy link
Author

tunggad commented Jun 20, 2011

hhmmm, i will try to find out where Play! local ivy repo is, perhaps the old version is still there.

@tunggad
Copy link
Author

tunggad commented Jun 20, 2011

Ok, the Play! ivy cache was the problem.

With refreshed .0.8.8.2 the $[reRenderSnip(reRender, dataType)] was correct converted to +reRenderSnip(reRender, dataType)+. But the $[testArray[0]]--> +testArr[0+"]

@branaway
Copy link
Owner

the array [] thing confused the parser. Let me think a bit.

@branaway
Copy link
Owner

get the 0.8.8.3 and start using $[ xxx ]$ as the inline expression.

@tunggad
Copy link
Author

tunggad commented Jun 20, 2011

I didnt see any changes with 0.8.8.3. What i see, is, i can still use the syntax $[xxx]. And i tried the new $[ xxx ]$ for the array but didnt see any change, is still converted to + testArr[0+" ]$

Must i use the syntax $[ xxx ]$ consequently for all usages?

@branaway
Copy link
Owner

hmm, I refreshed it just now. Can you verify that? Also, go to the japid cache and find the readme there see if you can see a log of it.

@tunggad
Copy link
Author

tunggad commented Jun 20, 2011

everthing is fine now, confirmed! Thank you very much!

@tunggad tunggad closed this as completed Jun 20, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants