Docs: Snippets

With Snippets you can create your own blocks of PHP executable code that can be displayed within your template header or footer html files. You insert the PHP into the top.inc file, which is then displayed in the header.html or footer.html files of your templates.

For example, a snippet of PHP in your top.inc file called $snippet1 will be parsed by the equivilent var in your template called {$snippet1}:

in your php/top.inc file: $snippet1 (or $snippet2 etc.)
in your template/header.html or template/footer.html file: {$snippet1} (or {$snippet2} etc.)

Example snippet 1: Welcome message

Here's an example of creating a snippet to be added to the header.html file:

In top.inc we write:

<?
if($_SESSION['first_name']) {
      $snippet1 = "<p>Hi $_SESSION['first_name'].";
      if($_SESSION['order_items']>0) $snippet1 .= "<br />You have $_SESSION['order_items'] in your cart.";
      $snippet1 .= "</p>";
} else {
      $snippet1 = "<p>You are not logged in.</p>";
}
?>

In your header.html template file we write:

<div id="side">
{$snippet1}
</div>

This will appear something like:

"Hi John, you have 3 items in your cart."

Example snippet 2: similar products list

Here's a snippet that displays similar products on the product page, to be added to the footer.html file. It queries the product table in the store.db SQLite database, then displays the products as a list.

In top.inc we write:

<?
if($_PAGE['subsection']=='product') {
$rs=sqlite_query($sDB,"select name,url,price from (product) where category_id='$_PAGE[category_id]' and status_id!='3' order by price asc limit 20");
      if(sqlite_num_rows($rs) > 0){
            $snippet2.= "<h3>Similar products</h3>\n<dl class=\"more-products\">\n ";
      while($data=sqlite_fetch_array($rs)){
       $ROWCLASS = ($ROWCLASS == 'r') ? 'r-a' : 'r';
            $snippet2.= " <dt class=\"$ROWCLASS\"><a href=\"/shop/$data[url]\">".$data['name']."</a></dt><dd class=\"$ROWCLASS\"><span class=\"highlight-price\">".cur_format($data['price'])."</span></dd>\n";
      }
      $cat_count = sqlite_fetch_single(sqlite_unbuffered_query($sDB,"SELECT count(*) FROM product WHERE category_id = '$CURRENT_CATEGORY_ID' and status_id!='3'"));
            $cat_link = $curl;
            if($parent_fid) $cat_link = $parent_fid."/".$curl;
            $snippet2.= "</dl>\n";
            if($cat_count>20) {
            $snippet2.= "<h4 style=\"clear:both;\"><a href=\"/shop/".$cat_link."\">View all $cat_count products...</a></h4>\n";
            }
      }
}
?>

In your footer.html template file we write:

<div id="similar_products">
{$snippet2}
</div>

This will appear something like:

Similar Products

Chocolate Fish
$25.00
Pineapple Lumps
$50.00



 

Revisions history (11)    ( hide | show )     Extensions > Snippets

×