I'm a web developer that recently moved from using PHP to Java for web applications. I ended up writing my own server application that was easily extendable using uncompiled groovy scripts that my server would evaluate through a Groovy Shell -- ie. foo.html.groovy
. To this day it works wonders and I'm very proud of my results... BUT... there is one thing I very much miss from PHP scripts, that I would like to implement inside my application and that's the ability to embed the programming language code inside the file. Not only do I miss it for convince, I also miss it because using many print
statements to output html, or similar, is quickly becoming a pain, not to mention that Groovy does not play well with JavaScript code unless almost everything is escaped -- ie. $ " '
.
PHP:
<p class="warning"><?php echo($result["error"]); ?></p>
Even Ruby on Rails offers something like this with .erb
files, plus I'd imagine something from Groovy would look like this:
<p class="warning"><%= result.error %></p>
I'm just curious if anyone knows if this has been implemented or if it could be emulated? When I was first developing my application I did make attempts at using a Regex formula to filter out the embed code, evaluate it, and replace it back in but this was not 100% flawless and it made debugging any code errors a nightmare since line and column numbers were never accurate. I did also try using Quercus -- which emulates PHP inside Java -- but their project is so outdated, I had I hard time finding documentation or help. So any ideas or direction to solutions would be much appreciated.
If anyone is interested in seeing what I did as reference, here is my code repository at GitHub -- FYI, my application is a bit overdue for somewhat of an overhaul to the code and structure.
Thanks again for any help.
The Grails (https://grails.org) framework allows for that sort of templating (among many other things). It's essentially the Groovy equivalent of Ruby on Rails (hence the name similarity -- it used to be known as Groovy on Grails).
Here's one of their introductory sample apps:
https://github.com/grails-samples/grails-petclinic
It lets you do things like this, within a <table>
tag:
<g:each var="v" in="${visit.pet.visits}">
<g:if test="${v.id}">
<tr>
<td><g:formatDate date="${v.date}" format="yyyy-MM-dd"/></td>
<td>${v.description}</td>
</tr>
</g:if>
</g:each>
Edit in response to comment from OP:
If Grails is too heavy-weight and different from what you currently have, you may want to give Groovy Templates a try.
These are built in to the language, much lighter weight than Grails, and much more similar to the PHP style you mentioned and the JSP Scriplets that were mentioned in another comment.
This is the example in the Groovy doc:
import groovy.text.SimpleTemplateEngine
def text = 'Dear "$firstname $lastname",\nSo nice to meet you in <% print city %>.\nSee you in ${month},\n${signed}'
def binding = ["firstname":"Sam", "lastname":"Pullara", "city":"San Francisco", "month":"December", "signed":"Groovy-Dev"]
def engine = new SimpleTemplateEngine()
template = engine.createTemplate(text).make(binding)
def result = 'Dear "Sam Pullara",\nSo nice to meet you in San Francisco.\nSee you in December,\nGroovy-Dev'
assert result == template.toString()