Lil Josh

A tech blog about web development with PHP, MySQL, jQuery, CSS3 and various other programming languages. You will not find anything about ColdFusion, ASP.NET, C# on this blog.

Velocity Cheat Sheet

October 28, 2013

Decided to make a velocity cheat sheet/quick start guide. Hopefully this is useful for anyone just starting out and also a good reference for experienced velocity developers. If I missed anything feel free to let me know.

## This line is a comment
## The next line is a variable assignment
#set($foo = "hello world")
## The next line uses the variable
## Velocity is good with dates
<p>Today is $_DateTool.getDate()</p>
## If your using velocity against XML data
## you can get a single node like so:
#set($singleNode = $_XPathTool.selectSingleNode('/path/to/node'))
## or you can select multiple nodes:
#set($multipleNodes = $_XPathTool.selectNodes('/path/to/nodes'))
## and iterate through the nodes like so:
#foreach ($node in $multipleNodes)
## You can use if statements like so:
## you can get the child nodes like so:

Also a useful reference chart provided by (

Description Usage Example
Variables #set() #set($foo = “hello”)
Comments (single line) ## ## Comment
Comments (multi lines) #* *#  #*
* Comments
* on multiple lines
Arithmetic Operators +   –   *   /   % #set($age = $age + 1)
(not just equivalency, can be used to compare objects)
== #if($gender == “female”)
Logic Operators &&   ||    ! #if($state == “Indiana” && $country == “USA”)
Range Operator
(used in loops)
Escape Character
Reference a variable $age

Jack is $age years old

Reference a variable
(if value is null, print nothing)
Assign a value to a variable
(string literal)
#set ( $!foo = “Velocity” )
Refer to a hashtable key
or a get (Address) method
Conditional Statement #if ()
 #if ($foo)





Loop #foreach ($criterion in $criteria) ## loop ArrayListCurrent Value: $!criterion#end
apache cascade server cheat sheet hannon hill velocity

Swapping variables in PHP

I’ve created a short and sweet PHP function swap() to exchange/swap the values of two PHP variables. Here it is in action:

$cat = "cat";
$dog = "dog";
echo "$cat goes meow and $dog goes woof"; //dog goes meow and cat goes woof
function swap(&$var1, &$var2) {
$tmp = $var1;
$var1 = $var2;
$var2 = $tmp;

If you’re running this function a lot (e.g. millions of iterations) it’s actually faster to just perform the swap without the function. For example:

for($i = 0; $i < 10000000; $i++) {
$tmp = $var1;
$var1 = $var2;
$var2 = $tmp;

Thanks @dnaslave for providing efficiency feedback! :]

function PHP swap variables

Matrix like background

October 17, 2013

<div id="background"></div>
var int=self.setInterval(function(){updateBackground()},100);
function updateBackground()
var content = "";
for(var i=0;i<2048;i++){
content += Math.round(Math.random()).toString();
#background {
background: black;
color: #0F0;

Sort HTML table using jQuery

September 14, 2013

I’ve put together a script that allows you to make any HTML table sortable by clicking on the table headings. See the example below:

Name Age
John Smith 18
Ashley Washington 26
Patrick Green 19

To use, download ljSortable.min.js and include it near the bottom of your page (after the table) and give your table an id of “ljSortable”. For example:

<table id="ljSortable">
<td>John Smith</td>
<td>Ashley Washington</td>
<td>Patrick Green</td>
<script src=""></script>
HTML jquery sort sortable sorting table