Quantcast
Channel: Lynk IT Blog » Template
Viewing all articles
Browse latest Browse all 2

Tutorial: TPL Compiler Part 2

$
0
0

Part 1 here

More Variables

Earlier I discussed system-wide variables (properties) that are available to use directly within within the CMS or templates. Properties such as {$IP} and {$year} are automatically assigned by the system, and you can easily assign other properties in PHP:

$theme->assign("Foo", "Bar");

It would be useful then, to be able to assign properties directly within the CMS/TPL as well. To do this, you must use the assign command. With this, you can assign more than just primitives and strings, you can assign functions and operators too. An example that uses assignments is the shopping cart:

{assign $showcategory=0}
{assign $lineprice=func math($line.price * $line.qty)}

Here, in the first line, we are just instructing the template to display a link to the product category in the shopping cart line or not, in this case not. The description in the cart would be for example "My Product" rather than "A category - My Product".

The second line is more interesting, here we are dynamically calculating the line total from the number of products in this line multiplied by the price of the product, and assigning it to a new property called $linetotal. Notice the dot notation used in $line.qty and $line.price - this is standard format for referring to inner array values in a multi-dimension array within a foreach block. More on this and the math function here.

We can now use the property $lineprice elsewhere in the content eg:

<td>Line Total:</td>
<td>{$lineprice}</td>

I know what youre thinking, why cant we just use:

<td>Line Total:</td>
<td>{func math($line.price * $line.qty)}</td>

This is perfectly valid. However, we may want to improve the usability by performing a comparison on the calculated value, for example if the value is zero, display a message eg: "TBC" instead of "0.00". Currently, we cannot perform compound if functions and so this would be impossible with the former construct. By using assignments, we can now perform this comparison:

{if $lineprice} £ {func number_format($lineprice,2)} {else} TBC {/if}

This is useful for when using the shopping cart as a quotation system rather than the standard checkout:

Putting it all together

The shopping cart makes use of a multi-dimension foreach, nested if constructs and the math function so is the ideal example to use here. To make it easier to follow Ive greyed out the HTML sections:

{if $cartcount}
   {foreach $lines as $pid => $line}
        {assign $showcategory=0}
        {assign $lineprice=func math($line.price*$line.qty)}

        <tr class="td0">
            <td class="cbody"><a href="{$basketpage}?{$cartvar}=del&pid={$pid}">
            <img src="img/trashicon.gif" alt="Remove item" title="Remove item"></a></td>

            <td class="cbody">
            <input name="newquan[]" type="text" id="newquan[]3" value="{$line.qty}">
            <input name="eid[]" type="hidden" id="eid[]" value="{$pid}">
            </td>
            <td class="cbody" colspan="2">
                {if $showcategory}<a href="{$line.clink}">{$line.catname}</a> - {/if}
            <a href="{$line.plink}">{$line.name}</a>
            </td>
            <td class="cbody">
               {if $line.price}&pound;{func number_format($line.price,2)}{else}TBC{/if}</td>
            <td class="ltotal">
               {if $lineprice}&pound;{func number_format($lineprice,2)}{else}TBC{/if}</td>
	</tr>
   {/foreach}

   <!--This bit removed for brevity -->

{else}
   <p>There are no items in your cart.</p>
{/if}

The script begins by evaluating the property $cartcount, which simply contains the number of items in the cart. If this is zero it skips to the else block and displays an appropriate message. Some other pre-defined properties are $basketpage and $cartvar - we could use absolute references here so long as we never intend to change the filenames and parameters.

You may also notice some sub-properties of the array: $line.clink and $line.plink. These are links back to the product page and category page respectively. Again we could use absolute references, but then these automagically construct the appropriate search engine friendly URL, which is very easy to get wrong if you were manually coding these. If you later turn off SEO URLs, or change the name/SEO link of the product, these would no longer work with manual coding, so why bother.

All that remains is to explain where these values came from. In the shopping basket PHP script, the following lines define everything displayed above:

$theme->assign("lines", $cart->toArray());
$theme->assign("cartcount", $cart->itemCount());

You may also notice some more PHP->TPL assignments that control other parts of the shopping cart, such as the Shipping, Subtotal, Order Total and VAT. If you have multiple Shipping zones set up, theres also some code to display the selector. The toArray() method simply packages the contents of our shopping cart up into a simple to use format, such as setting up the .clink and .plink sub-properties.


Viewing all articles
Browse latest Browse all 2

Trending Articles