| Building templates |
|---|
[ Back to top ]
| Preamble |
|---|
| iStrip uses a template system so you can easily change the look of the pages it generates without having to rewrite any of the PHP code. The template system used is Smarty, and it allows some very powerful template design features. This document aims to cover what you need to know about creating your own templates. The Smarty template engine is fully documented on the Smarty website so if you're in need of additional information then try the online Smarty documentation. |
[ Back to top ]
| Template design basics |
|---|
| An iStrip theme consists of a directory containing one
or more template files. All themes must provide at least an index.tpl file.
Themes are covered in more detail in the documentation
on themes. An iStrip template is a HTML file containing some special markup that allows iStrip to insert information into the HTML at the desired point called template variables, process arrays of template variables, include other files, hide certain parts of the HTML under certain circumstances and perform other useful functions. By default the characters used to delimit this special template markup are the curly brace characters, {}. Template variables and arrays always begin with the $ symbol, while calls to Smarty functions do not. For example, {$allowed_html} is a variable, while {truncate} is a function call. Template files have a .tpl extension by default. It is recommended that you look at the text of the supplied template files for more insight into the layout of an iStrip template. You can gain some useful information from these templates. |
[ Back to top ]
| Templates and Dreamweaver |
|---|
| As has already been stated, iStrip templates are basically
HTML files with some extra embedded template markup for accessing iStrip
functions. They can be opened and edited in any text editor or HTML editor.
Some products, such as Dreamweaver from Macromedia can be configured to
allow you to edit TPL files as if they were HTML files. This section details
how to configure Dreamweaver MX to read template files and treat them as
HTML. Additional information on Smarty and Dreamweaver can be found at
http://smarty.incutio.com/?page=ConfiguringDreamweaver In order to effectively use TPL files from within Dreamweaver, there are 3 main stages in configuration that should be taken:
|
[ Back to top ]
| Template variables |
|---|
| In order to display the contents of a given template
variable in a template, you give the name of the variable prepended with
a $ symbol surrounded by curly braces. For example, ($SCRIPT_NAME) will
give you the name of the PHP script that is using the template file. Most of the variables are provided in groups. These variables are accessed by giving the group name followed by the variable name, separated by the dot character. For example ($strip.id) will return the number of the current strip, {$archive.days_ago_first} will return the number of days that have elapsed since the first strip was uploaded, etc. For a full list of template variables and groups, see the documentation on template variables. |
[ Back to top ]
| Array variables |
|---|
| Some variable groups, such as the $striplist group,
are in fact indexed arrays. Each index can contain a value, as is the case
with the $themes group, or it can
contain a subgroup of variables, as is the case with the $striplist group.
To access index values in the case where each index is a value, you use
the group name followed by the index entry you want in square braces. For
example, {$themes[0]} will return the name of the first theme iStrip can
find in the themes directory. In the case where each index is a subgroup
of values, you use the group name, followed by the index you want in square
braces, followed by the name of the member of the subgroup you want. For
example, {$striplist[0].strip_title} will return the title of strip number
1. NOTE: In the case of the $striplist group, the index value is not the strip ID. It is, in fact, the strip ID minus one. So strip 1 is index 0, strip 2 is index 1, etc. This is because Smarty expects arrays to always be zero-indexed. If the array was one-indexed with index 1 being strip 1, many of the Smarty functions used for looping through arrays would cut off the final entry off. In order to avoid confusion, each $striplist group includes an id member with the real index number. For example. {$striplist[0].id} will return 1. Fortunately, it is not intended that you explicitly access the group index like that so the fact that strip 1 is at index 0 should normally not be an issue. Functions such as section and foreach are provided for traversing indexed groups. Example: {section name=row loop=$strip}This will generate a list of strip IDs and their respective titles. The sectionelse section will be displayed if there are no strips in the index. For more information on section and sectionelse, see the section on conditionals. |
[ Back to top ]
| Conditionals |
|---|
| The Smarty engine contains methods to allow you to display
different output depending on the state of template variables. The simplest
method of doing this is to use the default function.
This will let you set some text to display if the template variable you
specify contains no value. To use default, you give the name of a template
variable you want to display and follow it with |default:'Default text
to display' in the curly braces. For example, {$strip.title|default:'This
strip has no title'} will either show the specified strip title, or the
string supplied between the quotation marks if no title was set Section also provides conditional functionality. If you specify an array in section that contains no values then you can use the {sectionelse} tag to provide a default string to display. You can see an example of this in the section on array variables. Another conditional block provided by Smarty is the if block. This block takes the form {if condition}Text to display{/if} in it's most basic form. The if block also supports elseif and else to allow you to check for several different conditions. Example: This strip was uploadedThis will display how many days have elapsed since the strip being viewed was uploaded unless it was uploaded 0 days ago, in which case it will display "today", or if it was uploaded 1 day ago, in which case it will display "yesterday". Another way to use if is to check if a variable has been set. For example {if $striplist}The strip index contains entries{else}There are no strips in the strip index{/if} will display the first message if $striplist contains any value, without being concerned over what value it contains. If $striplist contains no value at all then the second message. The if block supports many different ways of checking and comparing variables, they are detailed more fully in the Smarty documentation on if. |
[ Back to top ]
| Other useful template features |
|---|
| In order to prevent spiders harvesting your email address
off your comic website, Smarty provides a mailto function
that will allow you to put an email address on your page but obfuscate
it in such a way as to prevent spiders from being able to read it. For
example, {mailto address='foo@bar.com' text='click here to send mail' encode='javascript'}
will generate a javascript link through which people can mail you. Your
address will be encoded into a meaningless string that mail address harvesters
cannot read. If you don't wish to use javascript encoding then there is
also a hex mode provided that can by used by setting encode to 'hex'. This
method also attempts to scramble your email address but doesn't depend
on javascript. This method will usually defeat mail harvesters, but some
will be able to decode your address. If you wish to include other PHP scripts in your templates then you can use the include_php function. For example, {include_php file='foo.php'} will call foo.php and display it's output at the point in the template where the include_php tag was written. You can also assign the output to a variable instead of displaying it if you wish. NOTE: paths will be relative to the iStrip script that is currently in use, not the template file Alternatively, you can include PHP code directly into your templates with the php function. For example {php}echo ('hello world');{/php} There are many other useful functions provided by the Smarty engine but I will not go into more detail here. For more information, see the Smarty documentation. |
[ Back to top ]
[ Back to top ]
[ Back to top ]
| Template Hints and Tips |
|---|
|
[ Back to top ]
Copyright © 2002, 2003 Gordon McVey