branaway / Japid

A Java-based statically-typed fast template engine that can be used in any Java code. It has special adapter for use with the Play! Framework.
113 stars 18 forks source link

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

Closed tunggad closed 13 years ago

tunggad commented 13 years ago
`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 commented 13 years ago

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 commented 13 years ago

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

tunggad commented 13 years ago

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 commented 13 years ago

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

tunggad commented 13 years ago

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 commented 13 years ago

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 commented 13 years ago

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

tunggad commented 13 years ago

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!

branaway commented 13 years ago

Let me think about it, OK?

branaway commented 13 years ago

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 commented 13 years ago

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

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

branaway commented 13 years ago

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 commented 13 years ago

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 commented 13 years ago
      `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 commented 13 years ago
`String[] testArray = {...};`
`String value = "Hello $[testArray[0]]";`

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

branaway commented 13 years ago

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

tunggad commented 13 years ago

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

branaway commented 13 years ago

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 commented 13 years ago

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

tunggad commented 13 years ago

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

tunggad commented 13 years ago

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 commented 13 years ago

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

branaway commented 13 years ago

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

tunggad commented 13 years ago

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 commented 13 years ago

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 commented 13 years ago

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