Tips & TricksBlank Variable Handling
There may be times when you want to print a default value for an empty
variable instead of printing nothing, such as printing " " so that
table backgrounds work properly. Many would use an
{if} statement to
handle this, but there is a shorthand way with Smarty, using the
default
variable modifier.
Printing when a variable is empty
See also default
and Default Variable Handling.
Default Variable Handling
If a variable is used frequently throughout your templates, applying
the default modifier every time it is mentioned can get a bit ugly. You
can remedy this by assigning the variable its default value with the
{assign} function.
Assigning a template variable its default value
See also default and
Blank Variable Handling.
Passing variable title to header template
When the majority of your templates use the same headers and footers, it
is common to split those out into their own templates and
{include} them.
But what if the header needs to have a different title, depending on
what page you are coming from? You can pass the title to the header when
it is included.
Passing the title variable to the header templatemainpage.tplarchives.tplheader.tpl{$title|default:"BC News"}
]]>
footer.tpl
]]>
When the main page is drawn, the title of "Main Page" is passed to the
header.tpl, and will subsequently be used as the title. When the
archives page is drawn, the title will be "Archives". Notice in the
archive example, we are using a variable from the
archives_page.conf
file instead of a hard coded variable. Also notice that "BC News" is
printed if the $title variable is not set, using the
default
variable modifier.
Dates
As a rule of thumb, always pass dates to Smarty as timestamps. This
allows template designers to use date_format for full
control over date formatting, and also makes it easy to compare dates if
necessary.
As of Smarty 1.4.0, you can pass dates to Smarty as unix
timestamps, mysql timestamps, or any date parsable by
strtotime().
using date_format
This will output:
This will output:
When using {html_select_date}
in a template, The programmer will most
likely want to convert the output from the form back into timestamp
format. Here is a function to help you with that.
converting form date elements back to a timestamp
]]>
See also
{html_select_date},
{html_select_time},
date_format
and $smarty.now,
WAP/WML
WAP/WML templates require a php
Content-Type header
to be passed along
with the template. The easist way to do this would be to write a custom
function that prints the header. If you are using
caching, that won't
work so we'll do it using the
{insert}
tag; remember {insert} tags are not
cached! Be sure that there is nothing output to the browser before the
template, or else the header may fail.
using {insert} to write a WML Content-Type header
]]>
your Smarty template must begin with the insert tag :
Welcome to WAP with Smarty!
Press OK to continue...
Pretty easy isn't it?
]]>
Componentized Templates
Traditionally, programming templates into your applications goes as
follows: First, you accumulate your variables within your PHP
application, (maybe with database queries.) Then, you instantiate your
Smarty object, assign() the variables and
display() the template. So lets
say for example we have a stock ticker on our template. We would
collect the stock data in our application, then assign these variables
in the template and display it. Now wouldn't it be nice if you could
add this stock ticker to any application by merely including the
template, and not worry about fetching the data up front?
You can do this by writing a custom plugin for fetching the content and
assigning it to a template variable.
componentized templatefunction.load_ticker.php -
drop file in
$plugins directory
assign($params['assign'], $ticker_info);
}
?>
]]>
index.tpl
See also {include_php},
{include} and
{php}.
Obfuscating E-mail Addresses
Do you ever wonder how your E-mail address gets on so many spam mailing
lists? One way spammers collect E-mail addresses is from web pages. To
help combat this problem, you can make your E-mail address show up in
scrambled javascript in the HTML source, yet it it will look and work
correctly in the browser. This is done with the
{mailto} plugin.
Example of Obfuscating an E-mail AddressTechnical Note
This method isn't 100% foolproof. A spammer could conceivably program his
e-mail collector to decode these values, but not likely....hopefully.
See also escape
and
{mailto}.