PHP Zone

How to call the function inside the Smarty Foreach Loop?

21 Oct , 2015  

Introduction to Smarty :

The Smarty design was largely driven by these goals:

  • Clean separation of presentation from application code
  • PHP backend, Smarty template frontend
  • complement PHP, not replace it
  • Fast development/deployment for programmers and designers
  • Quick and easy to maintain
  • Syntax easy to understand, no PHP knowledge required
  • Flexibility for custom development
  • Security: insulation from PHP
  • Free, open source

About Smarty :

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. This implies that PHP code is application logic, and is separated from the presentation.

Why is separating PHP from templates important?

There are many benefits of separating PHP code from templates, some of which are:

  1. SYNTAX:

    Templates typically consist of semantic markup such as HTML. PHP syntax works well for application code, but quickly degenerates when mixed with HTML. Smarty’s simple {tag} syntax is designed specifically to express presentation. Smarty focuses your templates on presentation and less on “code”. This lends to quicker template deployment and easier maintenance. Smarty syntax requires no working knowledge of PHP, and is intuitive for programmers and non-programmers alike.

  2. FEATURES:

    The template engine has many features for presentation that would otherwise need to be developed, tested and maintained in your own application code. Tags also mask the complexity of PHP statements

    For Eaxample :
    (a). In PHP

    <?php echo strtolower(htmlspecialchars($title,ENT_QUOTES,UTF-8)); ?>
    

    (b). In Smarty

    {$title|escape|lower}
    

    Both will results the same output.

  3. SANDBOXING:

    When PHP is mixed with templates, there are no restrictions on what type of logic can be injected into a template. Smarty insulates the templates from PHP, creating a controlled separation of presentation from business logic. Smarty also has security features that can further enforce granular restrictions on templates.

  4. PORTABILITY:

    Since Smarty templates are language-agnostic, they can easily be compiled to other languages (such as javascript) with a different compiler, and the familiar syntax can also be ported to other programming languages.

How does it work?

Under the hood, Smarty compiles copies of the templates as PHP scripts. This way you get the benefits of both template tag syntax and the speed of PHP. Compilation happens once when each template is first invoked, and then the compiled versions are used from that point forward. Smarty takes care of this for you, so the template designer just edits the Smarty templates and never has to manage the compiled versions. This approach keeps the templates easy to maintain, and yet keeps execution times extremely fast. Since the compiled versions are PHP, op-code accelerators such as APC or ZendCache continue to work on the compiled scripts.

Discussion :

Now I would like to call the function inside the foreach loop in smarty template, how to do that here is the php example with the smarty solution.

Example in PHP :

<?php
function array_combine_($keys, $values)
{
    $result = array();
    foreach ($keys as $i => $k) {
        $result[$k][] = $values[$i];
    }
    array_walk($result, create_function('&$v', '$v = (count($v) == 1)? array_pop($v): $v;'));
    return $result;
}
$country = array("India","United States","United Kingdom","India","Australia");
$city    = array("Coimbatore","Newyork","London","Bangalore","Melborne");
$output  = array_combine_($country, $city);
$get_array_keys = array_keys($output);
$get_array_values = array_values($output);
$get_count = count(array_keys($output));
for($i=0;$i<$get_count;$i++){
    if(is_array($get_array_values[$i])){
        echo "<b>".$get_array_keys[$i]."</b>&nbsp;-&nbsp;".implode(",",$get_array_values[$i])."<br>";
    }
    else{
        echo "<b>".$get_array_keys[$i]."</b>&nbsp;-&nbsp;".$get_array_values[$i]."<br>";
    }
}
?>

Expected Output :

India - Coimbatore,Bangalore
United States - Newyork
United Kingdom - London
Australia - Melborne

Solution in Smarty :

{php}
    function array_combine_(){
    }
{/php}
{foreach from=$companies item=record key=key}
    {$record.country}
    {$record.city}
    array_combine_({$record.country},{$record.city});
{/foreach}

John Peter

I’m professional web developer with 6+ years experience. My wide range of knowledge in web development using PHP, jQuery, WordPress, Magento, Symfony, Smarty and Web Scrapping. I am working with strong enthusiastic team (Group of friends) with spirit. So we provide all web development related solutions like HTML/CSS development, Web Graphic design and LOGO.

, , , ,