VaeQL brings more power to Vae - June 15, 2010

We are proud to announce our latest innovation to the Vae platform: VaeQL.  VaeQL stands for Vae Query Language and it is a new layer in the stack that handles the vae() PHP function and paths provided to VaeML tags via the path="" attribute.

VaeQL adds the ability to perform math operations, comparisons, if statements, variables, and PHP functions directly in your path="" attributes and any other place a path is used in Vae.  There's a lot here, so we'll break it down with some examples.

1.  Beef up your <v:if> statements.

Previously, you could only test for the existence of a field within a <v:if> statement.  You can now perform any kind of test, and you may also group tests together.  Let's start with a simple comparison.  This would test if the price structure is greater than 5:

<v:if path="price>5">

Yep, that's now valid VaeML.  <, <=, and >= also work.  You can also test equality and inequality:

<v:if path="name=='Kevin'">
<v:if path="name!='Kevin'">

You may also use a single equal sign for equality and the <> operator for inequality.  Whatever you prefer.  Now, let's get fancy:

<v:if path="(price+5)</home/featured_price">
This item is more than $5 less than the featured item price!
</v:if>

The math operators +, and - work as expected for add and subtract. Multiplication and division are also supported, though their operators are doubled: ** and //.  This is to prevent confusion, as otherwise, we wouldn't know if artist/123 meant the artist with ID 123, or the value of artist divided by 123. 

What if you wanted to test if the price is an even number?  Do this:

<v:if path="!(price%2)">

The % operator is the modulus operator, which represents the remainder from performing an integer division.  In our example, this will be 0 for even numbers.  We add the not operator (the exclamation mark) to take the reverse of this -- return true when the modulus is 0, and false otherwise.

You can also check for multiple conditions:

<v:if path="featured&&digital">This is a featured digital product</v:if>

&& is the symbol for and, || is the symbol for or.  You can also use the words 'and' and 'or' directly.  This would be the same thing as above:

<v:if path="featured and digital">This is a featured digital 
product</v:if>

2.  Adding conditional statements to other VaeML tags

VaeQL also supports the "inline-if" statement popular in many programming langauges.  It works like this:

<v:text path="featured ? featured_description : description" />

If the path featured evaluates to true (it's a checked checkbox or a text field with text in it, etc.), then we will display the featured_description.  Otherwise, just the regular description.

Combine this with the above, and we can make some pretty awesome statements:

<v:text path="(price>=100 and featured) ? 'SUPER VALUE!' : 'Good Value')" />

Note that you can use request variables within VaeQL as well:

<v:text path="($from == 'wholesale' ? wholesale_description : description)" />

Note that in VaeQL, variables correspond to the request variables sent to the page either via a POST or in the URL.  This corresponds (and is linked into) the $_REQUEST array in PHP.

3.  Built-in support for PHP Functions

Vae paths have always supported some functions such as now() and host().  These were built-in functions, and there were only a few available.  Now VaeQL supports the entire PHP function library, and you can even define your own functions in PHP and use them in VaeML.  All the old functions are there too.

Here are some examples:

<v:text path="substr(description, 10, 10)" />
<v:collection path="items[price<PRICECUTOFF()]">

Functions can take arguments, which can be paths, variables, functions, or a full VaeQL query in its own right.

VaeQL also supports "range queries", which will let you specify a range for values.  Use a colon and a function within a predicate.  This would search for items with prices between $10 and $50:

// in __verb.php:
function PRICERANGE() {
  return array(10, 50);
}<v:collection path="items[price:PRICERANGE()]">

4.  Go crazy!

All of these things can be combined and used in all sorts of ways.  Play with it and figure out what works best for your applications.

VaeQL is immediately available on all Vae sites.  As always, thanks for using Vae!