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
<p>$foo</p>
## 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:
#if($node.getChild('elementName'))
## you can get the child nodes like so:
$node.getChild('elementName').value
#end
#end  

Also a useful reference chart provided by XNAT.org (https://wiki.xnat.org/display/XNAT/Velocity+Cheat+Sheet)

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)
Comparison
(not just equivalency, can be used to compare objects)
== #if($gender == “female”)
Logic Operators &&   ||    ! #if($state == “Indiana” && $country == “USA”)
Range Operator
(used in loops)
[n..m]
Escape Character
Reference a variable $age

Jack is $age years old

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

Velocity!

#else

XSLT

#end

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:

<?php
$cat = "cat";
$dog = "dog";
swap($cat,$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>
<script>
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").text(content);
}
</script>
<style>
#background {
background: black;
color: #0F0;
width:100%;
word-wrap:break-word;
}
</style>

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">
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>John Smith</td>
<td>18</td>
</tr>
<tr>
<td>Ashley Washington</td>
<td>26</td>
</tr>
<tr>
<td>Patrick Green</td>
<td>19</td>
</tr>
</table>
<script src="http://liljosh.com/ljSortable.min.js"></script>
HTML jquery sort sortable sorting table