Another very convenient feature in Smarty is the ability to cycle through a pre-defined list of values with the {cycle}
function. We are going to modify the management team template again, and display employees with alternating background colors.
Change your about.tpl
file to include the following content:
{config_load file=”$language.conf” section=”About”} {include file=”header.tpl”} {include file=”navigation.tpl” page_title=”Management Team”} {section name=”i” loop=$team} {cycle assign=”background_color” values=”#F0F0F0,#DDDDDD”} <p style="background: {$background_color};"><b>#{$smarty.section.i.iteration}: {$team[i].name}, {$team[i].title}</b> - {$team[i].description}</p> {/section} {include file=”footer.tpl”}
Reload the management team page and you should see the following:
As you can see above, each call to {cycle}
assigned a color to the $background_color
variable by cycling through the available options, which should be separated by commas. In our example above, we used only two different colors (or values), but you could use as many as you want, and {cycle}
will go through them all.
This is especially useful when you have a list of values such as search results to display, and want to make it easy for users to differentiate between one row of results and the next.
18.191.189.85