I am looking at various libraries that can be used as a templating engine in my project and right now Apache Velocity looks like a good candidate. I have the following doubts regarding its usage:
Is it possible to specify a SQL database query in a template and use the querys' return value to fill a parameter?. I found the following example in the Velocity user guide:
Hello,
#set( $result = $query.criteria("name") )
Your username is $result.
However the guide does not explain much about executing SQL queries. Is it possible to define a SQL SELECT query which returns a value and assign this value to a variable in the template? I am wondering if something like the example below is possible?
Hello,
#set( $result = $executeQuery("SELECT name FROM user") )
Your username is $result.
Would be grateful if you could shed some light on this. Anyone kind enough to provide an example, or point me to a location where I can find additional documentation on this?
You would typically do something like #set ($oldIndex = $number. toNumber($oldIndex)) if you have the NumberTool present in your context, for instance. And that's it, $oldIndex contains an integer! Please note that the Integer.
Approach 1: The obvious approach: Run and check. Run Velocity against the template to be checked. Look at the output and see if it is what you desired. This must be the most primitive testing approach, but most people nowadays are too lazy and want this done by the computer.
Velocity is a server-side template language used by Confluence to render page content. Velocity allows Java objects to be called alongside standard HTML. If you are are writing a user macro or developing a plugin you may need to modify Velocity content.
I would recommend something like http://velosurf.sourceforge.net/ instead of directly embedding queries.
Velocity is a very lightweight templating engine, it has very little functionality by itself. It works by interpolating variables defined (by you) in a context into a template file. By default, the context is empty, meaning that there are no variables for the template to use. Velocity doesn't allow instantiating new objects of custom classes, all it allows you to use is string and number literals, plus maps and lists:
#set ($n = 5)
#set ($s = 'Hello New World!')
#set ($m = {'four' : 4, 'five' : $n})
#set ($a = ['x', 'y', 'z'])
You're responsible for making Velocity "smart" by populating the context with useful objects before interpolating. As a starting point, you can use some of the Velocity Tools. You can add your own tools, like an SQL tool that lets you run queries. Or you can use a higher level framework that uses velocity and which already offers a rich set of tools available in templates.
So, to answer your question, you can execute any SQL statement you want, as long as you add in the context an object that can execute such statements. You should read more about how to properly use Velocity in their developer guide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With