Weekends… How quickly they come and go…
Monday, April 19th, 2004All in all it was a pretty good weekend. I devoted some time on Sunday to playing around with the Google API and I quickly came up with a full google-clone minus paging. After playing with two of PEAR’ classes Pager and Pager_Sliding I was reminded of how hit and miss most of the PEAR libraries are. The PEAR libraries are focused on two slightly different approaches to solving paging with large sets of data. Both classes focuses on loading a larger subset of information but only showing a small subset of information. I was hoping more for something to facilitate loading the count of information found and then I’ll control which records need to be displayed. Needless to say, PEAR goes by the way side, and I’m back to writing my own.
After I had a few queries and some simple information printing on the screen, I decided to start working on adding it to the WordPress theme of my website (similiar to my news aggragator page). While WordPress is an excellent blog tool, the theme support in it has alot left to be desired. Instead I was forced to either grab a static copy of the site (yuck) or try to work with the “PHP” as a templating language approach.
I can safely say, after 5+ years of professional website development, there are those who get templating and layers and those who never will. After seeing this post on SitePoint’s PHP Forums, it reminds me of how tired I’ve grown of the age old debate. To sum it up, if you plan on a project being around for the long haul, a good template library is crucial to the long term maintainabilty and extendability of that project. With that said, I prefer to have a PHP logic page with 10 to 25 lines of code with an html template of 10 to 15 lines of html then I would at having a scattered mess of several hundred lines of mixed html + php code.
(Please forgive the lack of formatting below… WordPress didn’t handle parsing the code + html template as well as I would have liked.)
example.php
< ?php
include( 'bTemplate/bTemplate.php' );
include( 'SOAP_Google.php' );
$google = new SOAP_Google( 'YOUR_GOOGLE_API_KEY_HERE' );
$result = $google->search(
array(
‘query’ => ‘Google Term Here’
)
);
if (false !== $result)
{
foreach( $result->resultElements as $search_result )
{
$search_results[] = array(
‘URL’ => $search_result->URL,
‘cachedSize’ => $search_result->cachedSize,
‘directoryTitle’ => $search_result->directoryTitle,
‘hostName’ => $search_result->hostName,
‘relatedInformationPresent’ => $search_result->relatedInformationPresent,
’snippet’ => $search_result->snippet,
’summary’ => $search_result->summary,
‘title’ => $search_result->title
);
}
}
else
{
echo ‘Query failed.’;
}
$template =& new bTemplate();
$template->set( ’search_results’, $search_results );
echo $template->fetch( ‘templates/search_list.html’ );
?>
>
And the matching template:
< loop:search_results >
< p>
< a href='< tag:search_results[].URL />‘>< tag:search_results[].title />< /a>< br/>
< tag:search_results[].snippet />< br/>
< tag:search_results[].URL /> -
< tag:search_results[].cachedSize /> -
< a href='< tag:search_results[].URL />‘>Cached< /a> -
< a href='example.php?searchQuery=related:< tag:search_results[].URL />‘>Similar pages< /a>
< /p>
< /loop:search_results>
>>>>>>>>>>>>>>>>
While bTemplate is hardly the *ideal* Template language to use for all things, it does a really good job of handling arrays and loops in templates. While most Template libraries such as PHPLib force a programmer to focus more on the complexity of handling loops in loops, bTemplate allows you to submit your array to the template and it handles the looping logic. Ah, a template language that makes a complex / harder to maintain piece of code and simplifies it…
Some day I’ll have enough time to give this topic the attention it needs. For now, if WordPress was templated, I’d have a nice and pretty google search within mysite instead of a more simplistic google hack.