Question: I have a horizontal menu of five images. All of these 5 images have active and grayed state. When a particular image is active the rest 4 need to be grayed out. Similarly for the other images. 

I did it in jquery and that too the code is not so optimized and good. It is like this

```
    $("document").ready(function(){
  $("#imageidone").click(function() {
         $("#imageidone").attr("src","/path to image_active");
         $("#imageidtwo").attr("src","/path to image_grayed");
         $("#imageidthree").attr("src","/path to image_grayed");
         $("#imageidfour").attr("src","/path to image_grayed");
         $("#imageidfive").attr("src","/path to image_grayed");
   });
      $("#imageidtwo").click(function() {
        $("#imageidone").attr("src","/path to image_grayed");
        $("#imageidtwo").attr("src","/path to image_active");
        $("#imageidthree").attr("src","/path to image_grayed");
        $("#imageidfour").attr("src","/path to image_grayed");
        $("#imageidfive").attr("src","/path to image_grayed"); });
and so on for imageidthree, imageidfour, imageidfive
     });

```

How can I do it in a better way using CSS sprites or a more compact jqueryish way,

Thank You

Answer: 
PHP **is** a template engine already.  

so, it has shorter form for `echo` statement, especially for this purpose:

```
<?=$imgfile?>

```

considerable shorter and comparable to `{$imgfile}` 

Note that to make use of brackets, you'll have to devise alternatives for loops, conditions and other statements, which will complicate your life. 
while using PHP as a template, you'll be able to use built-in PHP operators, like `foreach` or `if` or include. 

So, it would be better to stick to `<?=$imgfile?>` syntax.

Just make sure you have `short_open_tags` setting turned on
In order to achieve this behavior you should use a template engine like [Smarty](http://www.smarty.net/) or write your own interpreter that will replace such expressions with the appropriate values.

e.g.

```
$buffer = 'Hello {$world}';
$world = "World";

if(preg_match_all("/{([^}]+)}/im", $buffer, $matches, PREG_SET_ORDER)) {
    foreach($matches as $match) {
        $expression = $match[0];
        $exactMatch = $match[1];

        if(defined($exactMatch)) {
            $buffer = str_replace($expression, constant($exactMatch), $buffer);
        } else {

            if(strrpos($exactMatch, "$") !== false) {
                $vars = get_defined_vars();
                $var = str_replace("$", "", $exactMatch);
                if(isset($vars[$var])) {
                    $buffer = str_replace($expression, $vars[$var], $buffer);
                }
            }

            if(is_callable($exactMatch)) {
                $buffer = str_replace($expression, call_user_func($exactMatch), $buffer);
            }
        }
    }
}

echo $buffer;

```