## Introduction to Smarty Tags ##
Smarty templates are nothing more than regular HTML with special tags to control the displaying of dynamic data, in our case things like the the article’s title and contents on a Article page, or the listing of sub-categories on a Category page. With that in mind, we’ll dive straight into a small tutorial on the basics of Smarty markup.
### Smarty tags ###
Smarty tags are enclosed in `{ }` characters. Anything you put between these two characters is not shown directly in the page, but is processed by the Smarty engine.
### Variables ###
Smarty variables (for the most part) are assigned from the PHP script before loading the template. They are used to hold dynamic data, and can be simple things like words or numbers, but also more complex things like lists. For example, the Smarty variable `$site_name` is available to every context and contains the name of your site that you configured in the Admin section.
To display the contents of a simple variable in your HTML just put the name of the variable inside a set of smarty tags, like this: `{$site_name}`. So if you wanted to set the title of a page to your site’s name, you could do this:
[html]
[/html]
And when the page is shown, the actual HTML will come out like this:
[html]
[/html]
### Lists ###
*Lists* (or Arrays as they are sometimes called) are a more complex type of variable in Smarty. Instead of holding one value, *lists* hold multiple values and Smarty provides ways of looping over each value in the list. The most common use of *lists* in KB is for the `category.htm` template, which has lists of subcategories and articles for the current category you are viewing.
One way to loop over all of the values in a *list* is the Smarty `{section} … {/section}` block. Here’s an example from the `category.htm` template:
[html num=23]{section name=cat loop=$categories}
{else}{if $categories[cat].description ne “”}
{/if}
{* Every third time we want a new row *}
{cycle assign=tempvalue values=”,,
{$tempvalue}
{/section}[/html]
Now, there are lots of things going on in that block of HTML, but for now we’re only going to focus on the `{section}` block. At the very least (and there are more options that can be found on the [Smarty Documentation][1] site) the `{section}` block needs two parameters in the opening statement: `name` and `loop`. `loop` is the name of the *list* variable that holds the items you want to loop through, and `name` is the name of your section. The name of your section is very important in the block of HTML inside.
What a `{section}…{/section}` block does is print out the everything in between the `{section}` and `{/section}` tags once for every value in the list you specify with the `loop` parameter. So if our `$categories` list has 5 categories in it, the example `{section}` will print out all that HTML 5 times. This is where the `name` parameter comes in. Each time the HTML is printed out, you want to display information from the next value in the `$categories` list, so that you end up displaying information about all 5 categories. Every time through the loop you can access the current value from the list by putting the name of the section in `[]` brackets after the list name, like so: `$categories[cat]`. Each time through the loop, `$categories[cat]` will be a different category in the list of categories.
As for the other things in the example, we’ll cover `{if}` in a bit, and documentation on `{cycle}` can be found [here][2].
### Objects ###
Besides simple variables that contain things like words and numbers a lot of the Smarty variables used in KB hold more complex data in the form of *Objects* and *lists*. An *Object* is like a container for information, a single entity that holds various *properties*. Take the concept of an Article for example. An article has all sorts of information associated with it, things like it’s title, contents, author, creation date, number of times it’s been viewed, a rating, etc. In KB, the Article smarty object has the following properties:
* id - The ID of the article
* category - The ID of the category that the article is in
* question - The question
* content - The answer
* author - The name of the author
* authorid - The ID of the author
* date - The creation date for the article
* datemodified - The date the article was last modified
* numviews - The number of times the article has been viewed
* locked - Whether or not the article is “locked”
* numstars - The rating of the article (1 - 5)
To access a *Object’s* properties use the following structure: `$variablename.property`. So if you have an article held in the variable `$articleInfo`, and you want to display the question in bold you could do the following:
[html]{$articleInfo.question}[/html]
### Conditionals ###
One of the more powerful aspects of Smarty is it’s *conditionals*: the ability to dynamically determine if you should or shouldn’t print something out based on some property or variable. For example, in the *Category* context, you only want to display the table of sub categories if the current category actually _has_ sub categories right? Well, in the *category* context, there is a Smarty variable named `$num_categories` that holds the number of child categories, so we wrap the entire block of HTML that displays subcategories in a conditional, like so:
[html]{if $num_categories > 0}
{/if}[/html]
Now, every time the page loads to display a category, if there aren’t any sub categories, everything between the `
