I did some coding yesterday to run a 2014 World Cup confidence pool on SportsFilter. The way a confidence pool works is you assign points to teams based on how confident you are they will win. In this contest, you get point values from 32 down to 1 and assign them to the 32 World Cup teams. The more points you assign a team, the more you are awarded when that team wins or ties a match.
Anyone can sign up for a SportsFilter account and play. A prize will be awarded to the winner: the Adidas replica version of the official World Cup ball. The deadline for entries is shortly before the first match Thursday.
I wrote the web application in PHP, stored the database in MySQL and used the Smarty PHP template engine for the presentation. This was a project I had to complete in a single day, so I didn't think I'd go to the trouble of using Smarty. I figured I would just kludge together some ugly PHP pages that had code and HTML markup mixed together.
But Smarty makes it easier to present the HTML output of a PHP web application. I find that I write better PHP code when I use it and there's less debugging required to get things right.
The way Smarty works is you write a PHP script that does the work such as collecting user input and storing data, make some variables from that script available to Smarty and tell it to load a template file that contains HTML and some simple code to present information.
Here's the last part of the script that displays the contest entry form and validates what the user has submitted:
// make data available to templates
$smarty->assign('spofi', $spofi);
$smarty->assign('page_title', "Enter the SportsFilter World Cup 2014 Confidence Pool");
$smarty->assign('spofi', $spofi);
$smarty->assign('form_output', $form_output);
$smarty->assign('error_message', $error_message);
$smarty->assign('random_team', $random_team);
$smarty->assign('submit_widget', $submit_widget);
// display output
$smarty->display('header.tpl');
$smarty->display('confidence-pool.tpl');
$smarty->display('footer.tpl');
Those three .tpl files are templates. The header and footer contain the SportsFilter layout and the main body of the page is in confidence-pool.tpl.
Here's a portion of that template:
{$error_message}
<p><form action="/confidence-pool.php" method="post">
{$form_output}
<input type="submit" value="Preview My Entry" name="command">
{$submit_widget}
</form>
Smarty's template language can do more than display variables. It has for loops to display every element in an array and formatting functions that can do things such as control how the elements of a date are displayed.
I also used it on Wargames.Com. Here's some template code from the state directory pages:
{foreach from=$stores item=store}
<p class="directorystore">
<span class="directorystorename"><a href="{$store.link}">{$store.name}</a><br /></span>
{if $store.phone neq ''}<span class="directorystorephone">{$store.phone}<br /></span>{/if}
<span class="directorystorelocation">{$store.city}, {$store.statename} {$store.zip}<br /></span>
</p>
{/foreach}
The $stores variable is an array with link, name, phone, city, statename and zip elements.
The ultimate goal of today's project is to make it possible for a website's users to create and run their own confidence pools for any sporting event that lends itself to this type of contest. One sport it would be ideal for is the NFL, with players ranking the teams playing that weekend and competing all season.
All comments are moderated before publication. These HTML tags are permitted: <p>, <b>, <i>, <a>, and <blockquote>. This site is protected by reCAPTCHA (for which the Google Privacy Policy and Terms of Service apply).