Prev | Current Page 554 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

",
"Softball-sized hail reported in Wisconsin."
);
$smarty = new Smarty;
$smarty->assign("summaries", $summaries);
$smarty->display("truncate.tpl");
The truncate.tpl template contains the following:
{foreach from=$summaries item=summary}
{$summary|truncate:35:"..."}

{/foreach}
484 CHAPTER 19 ?–  T EMPLAT I NG WITH SMARTY
This returns the following:
Snow expected in the Northeast...
Sunny and warm weather expected...
Softball-sized hail reported in...
Control Structures
Smarty offers several control structures capable of conditionally and iteratively evaluating
passed-in data. These structures are introduced in this section.
The if Function
Smarty??™s if function operates much like the identical function in the PHP language.
As with PHP, a number of conditional qualifiers are available, all of which are displayed
here:
A simple example follows:
{* Assume $dayofweek = 6. *}
{if $dayofweek > 5}

Gotta love the weekend!


{/if}
??? eq ??? is not odd
??? gt ??? div by
??? gte ??? even by
??? ge ??? not
??? lt ??? mod
??? lte ??? odd by
??? le ??? ==
??? ne ??? !=
??? neq ??? >
??? is even ??? <
??? is not even ??? <=
??? is odd ??? >=
CHAPTER 19 ?–  TEMPLAT ING WITH SMARTY 485
Consider another example. Suppose you want to insert a certain message based on
the month. The following example uses conditional qualifiers and elseif and else to
carry out this task:
{if $month < 4}
Summer is coming!
{elseif $month ge 4 && $month <= 9}
It's hot out today!
{else}
Brrr.


Pages:
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566