Download DB2

Behold, the Web 2.0 Starter Toolkit for DB2!

August 9th, 2007 by Leons Petrazickis

The Starter Toolkit is an easy way to deploy a WAD-on-PHP stack – Windows Apache DB2 PHP. It includes some neat functionality for generating Atom feeds from XML data in your DB2 databases, and some excellent REST-ful web service wrappers from tables and stored procedures.

I’ve spent my last few months tinkering with it, so do take a look. There should be a few posts about it coming down the line, from both external and internal perspectives.
Install Apache, DB2, PHP, and lots of goodiesControl Panel MenuCreate Atom FeedsCreate REST-ful PHP web services
How is it different from Zend Core for IBM? Well, the toolkit has the Atom stuff, and the web services stuff, and a bunch of helpful resources to help folks get started making apps full of Web 2.0-ey goodness.

We’ll be pushing out a fresh revision in double-time, so give me some feedback, consarnit!:-)

Posted in javascript, php, db2 | 1 Comment »

Migrating PHP templates from Smarty to Flexy

March 20th, 2007 by Leons Petrazickis

Templates are a great way to separate business logic from page layout in web applications. Smarty (LGPL) and Flexy (PHP License) are two great, mature templating libraries for PHP. Flexy is the younger and faster of two, so there is a tendency to migrate installed Smarty implementations onto it.

Migrating the PHP

Engine Declaration

Smarty:

$smarty = new Smarty();

Flexy:

$smarty = new HTML_Template_Flexy_SmartyAPI();

This API is largely limited to emulating the assign() method.

Engine Configuration

Smarty:

$smarty->template_dir = ‘./smarty/templates’;
$smarty->compile_dir = ‘./smarty/templates_c’;
$smarty->cache_dir = ‘./smarty/cache’;
$smarty->config_dir = ‘./smarty/config’;

Flexy:

$options = array();
$options[‘templateDir’][] = dirname(__FILE__) . \\flexy\templates’;
$options[‘compileDir’] = dirname(__FILE__) . \\flexy\templates_c’;

Your templates go into the templateDir. Flexy will compile them to optimized PHP code and cache it in compileDir.

Page Display

Smarty:

$smarty->display($template);

Flexy:

$object = new StdClass;
foreach($smarty->vars as $k=>$v) {
        $object->$k = $v;
}

$flexy = new HTML_Template_Flexy($options);
$flexy->compile($template);
$flexy->outputObject($object, array());

Needless to say, this code would be much simpler if you were writing a web app from scratch. The goal during migration is to minimize points of change, which is achieved by slightly baroque markup at selected points rather than elegant markup across the board.

Migrating the Templates

Unfortunately, Flexy’s syntax differs from that of Smarty. I would recommend doing a search-and-replace with regular expressions across all your templates. On Windows, UltraEdit offers this across multiple files. On Unix, a script could be readily written.

Variables

Smarty:

{$blurb}

Flexy:

{blurb:h}
{blurb} // escaped HTML
{blurb:u}       // URL-encoded

Unlike Smarty, Flexy escapes HTML tags by default. It’s arguably more secure, but I find that I need to insert raw HTML into a template fairly often.

Array Loops

Smarty:

{section name=t loop=$tables}
        <li>{$tables[t]}</li>
{/section}

Flexy:

{foreach:tables,key,value}
        <li>{value:h}</li>
{end:}

Lower-case t should be avoided as a variable name in Flexy.

Javascript Variables

Smarty:

<script type="text/javascript">
        var db = ‘{$db}’;
</script>

Flexy:

<flexy:toJavascript db="db">

This is a useful way to transfer a variable from PHP to your client-side Javascript. It comes especially handy with Javascript strings.

Includes

Smarty:

{include file=‘0top.tpl’}

Flexy:

<flexy:include src="0top.tpl">

Documentation

Flexy Home
Flexy Documentation

This covers the minimum needed to migrate from Smarty to Flexy. I highly recommend abstracting and encapsulating some of these operations for ease of maintenance. It’s best to avoid repeating yourself.

Posted in php | No Comments »

When all you have is a hammer, perhaps you should contemplate acquiring a table saw

February 6th, 2007 by Leons Petrazickis

I had a an application that parsed an XML document and transformed it into a dojo.widget.Tree. This is how long it took with documents sized 15k, 34k, and 93k:


IE6 IE7 Firefox2 Opera9
15k 24 6 11 6
34k 69 11 21 21
93k 157 124

“Hmm,” I thought. “I don’t really need it to be collapsible or fancy. What if I replace the tree with a sequence of nbsp-indented lines and put that in via innerHTML?”


IE6 IE7 Firefox2 Opera9
15k 5 6 4 3
34k 14 11 19 10
93k

Then Internet Explorer 6 doesn’t look as pathetic as it is.

But why am I doing data munging in Javascript? The browsers seem to very unhappy about loading 93 kilobytes of XML into DOM. Would it be faster in PHP?


IE6 IE7 Firefox2 Opera9
15k 1 1 1 1
34k 1 1 1 1
93k 5 2 6 3

Yes. Yes, it would. 80x faster, in fact.

In conclusion, currently PHP is far faster for hundreds of kilobytes of data munging. Javascript is optimized for something else entirely — page management. Use it to move data around, but don’t transform with it.

Posted in javascript, php | No Comments »

PHP Smorgasboard

February 5th, 2007 by Leons Petrazickis

Links

Atomized | PHP Performance Best Practices - Informative.

Installing PEAR and PECL on Zend Core for IBM

  1. Save http://go-pear.org/ to go-pear.php (in, say, C:\Program Files\Zend\Core for IBM\pear)
  2. Open Command Prompt
  3. % cd “C:\Program Files\Zend\Core for IBM\pear”
  4. % ../bin/php go-pear.php
  5. Follow the steps and let it modify your php.ini
  6. Restart your Apache

Installing the profiling packages mentioned in Best Practices

  1. % pear install Benchmark
  2. % pecl install apd

Posted in php | No Comments »