Index: distribution/libs/sysplugins/smarty_internal_compile_foreach.php
===================================================================
--- distribution/libs/sysplugins/smarty_internal_compile_foreach.php	(revision 3654)
+++ distribution/libs/sysplugins/smarty_internal_compile_foreach.php	(working copy)
@@ -23,7 +23,7 @@
     {
         $this->compiler = $compiler;
         $this->required_attributes = array('from', 'item');
-        $this->optional_attributes = array('name', 'key');
+        $this->optional_attributes = array('name', 'key', 'max');
         $tpl = $compiler->template; 
         // check and get attributes
         $_attr = $this->_get_attributes($args);
@@ -74,6 +74,14 @@
         $usesPropIteration = $usesPropLast || strpos($tpl->template_source, $ItemVarName . 'iteration') !== false;
         $usesPropShow = strpos($tpl->template_source, $ItemVarName . 'show') !== false;
         $usesPropTotal = $usesSmartyTotal || $usesPropLast || strpos($tpl->template_source, $ItemVarName . 'total') !== false; 
+
+        if (isset($_attr['max']) && is_numeric($_attr['max'])) {
+            $maxIterations = $_attr['max'];
+            $usesPropIteration = true;
+        } else {
+            $maxIterations = null;
+        }
+
         // generate output code
         $output = "<?php ";
         $output .= " \$_smarty_tpl->tpl_vars[$item] = new Smarty_Variable;\n";
@@ -105,6 +113,9 @@
         } 
         $output .= "if (count(\$_from) > 0){\n";
         $output .= "    foreach (\$_from as \$_smarty_tpl->tpl_vars[$item]->key => \$_smarty_tpl->tpl_vars[$item]->value){\n";
+        if ($maxIterations != null) {
+            $output .= " if (\$_smarty_tpl->tpl_vars[$item]->iteration >= $maxIterations) { break; }\n";
+        }
         if ($key != null) {
             $output .= " \$_smarty_tpl->tpl_vars[$key]->value = \$_smarty_tpl->tpl_vars[$item]->key;\n";
         } 
Index: development/PHPunit/CompileForeachTests.php
===================================================================
--- development/PHPunit/CompileForeachTests.php	(revision 3654)
+++ development/PHPunit/CompileForeachTests.php	(working copy)
@@ -59,6 +59,21 @@
         $tpl = $this->smarty->createTemplate('string:{foreach item=x name=foo from=[0,1,2,3,4,5,6,7,8,9]}{$smarty.foreach.foo.index}{$smarty.foreach.foo.iteration}{foreachelse}else{/foreach}');
         $this->assertEquals("011223344556677889910", $this->smarty->fetch($tpl));
     } 
+    public function testForeachMax1()
+    {
+        $tpl = $this->smarty->createTemplate('string:{foreach item=x item=x from=[0,1,2,3,4,5,6,7,8,9] max=5}{$x}{/foreach}');
+        $this->assertEquals("01234", $this->smarty->fetch($tpl));
+    }
+    public function testForeachMax2()
+    {
+        $tpl = $this->smarty->createTemplate('string:{foreach item=x item=x from=[0,1,2,3,4,5,6,7] max=20}{$x}{/foreach}');
+        $this->assertEquals("01234567", $this->smarty->fetch($tpl));
+    }
+    public function testForeachMax3()
+    {
+        $tpl = $this->smarty->createTemplate('string:{foreach item=x item=x from=[0,1,2,3,4,5,6,7,8,9] max=-1}{$x}{/foreach}');
+        $this->assertEquals("", $this->smarty->fetch($tpl));
+    }
     /**
     * test {foreach $foo as $x} tag
     */
