mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-05 10:54:27 +02:00
Added ability to index by key.
This commit is contained in:
@@ -392,7 +392,7 @@ class Smarty
|
|||||||
|
|
||||||
/* If the tag name matches a variable or section property definition,
|
/* If the tag name matches a variable or section property definition,
|
||||||
we simply process it. */
|
we simply process it. */
|
||||||
if (preg_match('!^\$(\w+/)*\w+(?>\|@?\w+(:[^|]+)?)*$!', $tag_command) || // if a variable
|
if (preg_match('!^\$(\w+/)*\w+(?>\.\w+)*(?>\|@?\w+(:[^|]+)?)*$!', $tag_command) || // if a variable
|
||||||
preg_match('!^#(\w+)#(?>\|@?\w+(:[^|]+)?)*$!', $tag_command) || // or a configuration variable
|
preg_match('!^#(\w+)#(?>\|@?\w+(:[^|]+)?)*$!', $tag_command) || // or a configuration variable
|
||||||
preg_match('!^%\w+\.\w+%(?>\|@?\w+(:[^|]+)?)*$!', $tag_command)) { // or a section property
|
preg_match('!^%\w+\.\w+%(?>\|@?\w+(:[^|]+)?)*$!', $tag_command)) { // or a section property
|
||||||
settype($tag_command, 'array');
|
settype($tag_command, 'array');
|
||||||
@@ -854,11 +854,11 @@ class Smarty
|
|||||||
allow people to use older versions of PHP we emulate preg_grep() and
|
allow people to use older versions of PHP we emulate preg_grep() and
|
||||||
use the version check to see what function to call. */
|
use the version check to see what function to call. */
|
||||||
if (strnatcmp($PHP_VERSION, '4.0.4') >= 0) {
|
if (strnatcmp($PHP_VERSION, '4.0.4') >= 0) {
|
||||||
$var_exprs = preg_grep('!^\$(\w+/)*\w+(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
$var_exprs = preg_grep('!^\$(\w+/)*\w+(?>\.\w+)*(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
||||||
$conf_var_exprs = preg_grep('!^#(\w+)#(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
$conf_var_exprs = preg_grep('!^#(\w+)#(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
||||||
$sect_prop_exprs = preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
$sect_prop_exprs = preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
||||||
} else {
|
} else {
|
||||||
$var_exprs = $this->_preg_grep('!^\$(\w+/)*\w+(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
$var_exprs = $this->_preg_grep('!^\$(\w+/)*\w+(?>\.\w+)*(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
||||||
$conf_var_exprs = $this->_preg_grep('!^#(\w+)#(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
$conf_var_exprs = $this->_preg_grep('!^#(\w+)#(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
||||||
$sect_prop_exprs = $this->_preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
$sect_prop_exprs = $this->_preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
||||||
}
|
}
|
||||||
@@ -887,13 +887,17 @@ class Smarty
|
|||||||
$modifiers = explode('|', substr($var_expr, 1));
|
$modifiers = explode('|', substr($var_expr, 1));
|
||||||
|
|
||||||
$sections = explode('/', array_shift($modifiers));
|
$sections = explode('/', array_shift($modifiers));
|
||||||
$var_name = array_pop($sections);
|
$props = explode('.', array_pop($sections));
|
||||||
|
$var_name = array_shift($props);
|
||||||
|
|
||||||
$output = "\$$var_name";
|
$output = "\$$var_name";
|
||||||
|
|
||||||
foreach ($sections as $section) {
|
foreach ($sections as $section) {
|
||||||
$output .= "[\$_sections['$section']['properties']['index']]";
|
$output .= "[\$_sections['$section']['properties']['index']]";
|
||||||
}
|
}
|
||||||
|
foreach ($props as $prop) {
|
||||||
|
$output .= "['$prop']";
|
||||||
|
}
|
||||||
|
|
||||||
$this->_parse_modifiers($output, $modifiers);
|
$this->_parse_modifiers($output, $modifiers);
|
||||||
|
|
||||||
|
@@ -10,11 +10,8 @@ $smarty->assign("LastName",array("Doe","Smith","Johnson","Case"));
|
|||||||
$smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
|
$smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
|
||||||
array("I", "J", "K", "L"), array("M", "N", "O", "P")));
|
array("I", "J", "K", "L"), array("M", "N", "O", "P")));
|
||||||
|
|
||||||
$smarty->assign("Contacts",array(
|
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
|
||||||
"phone" => "555-4444",
|
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
|
||||||
"fax" => "555-3333",
|
|
||||||
"cell" => "760-1234"
|
|
||||||
));
|
|
||||||
|
|
||||||
|
|
||||||
$smarty->display("./templates/index.tpl");
|
$smarty->display("./templates/index.tpl");
|
||||||
|
@@ -25,13 +25,12 @@ My interests are:
|
|||||||
none
|
none
|
||||||
{/section}
|
{/section}
|
||||||
|
|
||||||
Contacts: {$Contacts}<br>
|
|
||||||
testing
|
testing
|
||||||
{$Contacts["phone"]["cell"]}<br>
|
{section name=sec1 loop=$contacts}
|
||||||
{$Contacts["phone"]["fax"]}<br>
|
phone: {$sec1/contacts.phone}
|
||||||
{$Contacts["phone"]["home"]}<br>
|
fax: {$sec1/contacts.fax}
|
||||||
{$Contacts["phone"]["work"]}<br>
|
cell: {$sec1/contacts.cell}
|
||||||
{$Contacts["fax"]}<br>
|
{/section}
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
|
@@ -10,11 +10,8 @@ $smarty->assign("LastName",array("Doe","Smith","Johnson","Case"));
|
|||||||
$smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
|
$smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
|
||||||
array("I", "J", "K", "L"), array("M", "N", "O", "P")));
|
array("I", "J", "K", "L"), array("M", "N", "O", "P")));
|
||||||
|
|
||||||
$smarty->assign("Contacts",array(
|
$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
|
||||||
"phone" => "555-4444",
|
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
|
||||||
"fax" => "555-3333",
|
|
||||||
"cell" => "760-1234"
|
|
||||||
));
|
|
||||||
|
|
||||||
|
|
||||||
$smarty->display("./templates/index.tpl");
|
$smarty->display("./templates/index.tpl");
|
||||||
|
@@ -392,7 +392,7 @@ class Smarty
|
|||||||
|
|
||||||
/* If the tag name matches a variable or section property definition,
|
/* If the tag name matches a variable or section property definition,
|
||||||
we simply process it. */
|
we simply process it. */
|
||||||
if (preg_match('!^\$(\w+/)*\w+(?>\|@?\w+(:[^|]+)?)*$!', $tag_command) || // if a variable
|
if (preg_match('!^\$(\w+/)*\w+(?>\.\w+)*(?>\|@?\w+(:[^|]+)?)*$!', $tag_command) || // if a variable
|
||||||
preg_match('!^#(\w+)#(?>\|@?\w+(:[^|]+)?)*$!', $tag_command) || // or a configuration variable
|
preg_match('!^#(\w+)#(?>\|@?\w+(:[^|]+)?)*$!', $tag_command) || // or a configuration variable
|
||||||
preg_match('!^%\w+\.\w+%(?>\|@?\w+(:[^|]+)?)*$!', $tag_command)) { // or a section property
|
preg_match('!^%\w+\.\w+%(?>\|@?\w+(:[^|]+)?)*$!', $tag_command)) { // or a section property
|
||||||
settype($tag_command, 'array');
|
settype($tag_command, 'array');
|
||||||
@@ -854,11 +854,11 @@ class Smarty
|
|||||||
allow people to use older versions of PHP we emulate preg_grep() and
|
allow people to use older versions of PHP we emulate preg_grep() and
|
||||||
use the version check to see what function to call. */
|
use the version check to see what function to call. */
|
||||||
if (strnatcmp($PHP_VERSION, '4.0.4') >= 0) {
|
if (strnatcmp($PHP_VERSION, '4.0.4') >= 0) {
|
||||||
$var_exprs = preg_grep('!^\$(\w+/)*\w+(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
$var_exprs = preg_grep('!^\$(\w+/)*\w+(?>\.\w+)*(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
||||||
$conf_var_exprs = preg_grep('!^#(\w+)#(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
$conf_var_exprs = preg_grep('!^#(\w+)#(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
||||||
$sect_prop_exprs = preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
$sect_prop_exprs = preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
||||||
} else {
|
} else {
|
||||||
$var_exprs = $this->_preg_grep('!^\$(\w+/)*\w+(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
$var_exprs = $this->_preg_grep('!^\$(\w+/)*\w+(?>\.\w+)*(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
||||||
$conf_var_exprs = $this->_preg_grep('!^#(\w+)#(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
$conf_var_exprs = $this->_preg_grep('!^#(\w+)#(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
||||||
$sect_prop_exprs = $this->_preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
$sect_prop_exprs = $this->_preg_grep('!^%\w+\.\w+%(?>\|@?\w+(:[^|]+)?)*$!', $tokens);
|
||||||
}
|
}
|
||||||
@@ -887,13 +887,17 @@ class Smarty
|
|||||||
$modifiers = explode('|', substr($var_expr, 1));
|
$modifiers = explode('|', substr($var_expr, 1));
|
||||||
|
|
||||||
$sections = explode('/', array_shift($modifiers));
|
$sections = explode('/', array_shift($modifiers));
|
||||||
$var_name = array_pop($sections);
|
$props = explode('.', array_pop($sections));
|
||||||
|
$var_name = array_shift($props);
|
||||||
|
|
||||||
$output = "\$$var_name";
|
$output = "\$$var_name";
|
||||||
|
|
||||||
foreach ($sections as $section) {
|
foreach ($sections as $section) {
|
||||||
$output .= "[\$_sections['$section']['properties']['index']]";
|
$output .= "[\$_sections['$section']['properties']['index']]";
|
||||||
}
|
}
|
||||||
|
foreach ($props as $prop) {
|
||||||
|
$output .= "['$prop']";
|
||||||
|
}
|
||||||
|
|
||||||
$this->_parse_modifiers($output, $modifiers);
|
$this->_parse_modifiers($output, $modifiers);
|
||||||
|
|
||||||
|
@@ -25,13 +25,12 @@ My interests are:
|
|||||||
none
|
none
|
||||||
{/section}
|
{/section}
|
||||||
|
|
||||||
Contacts: {$Contacts}<br>
|
|
||||||
testing
|
testing
|
||||||
{$Contacts["phone"]["cell"]}<br>
|
{section name=sec1 loop=$contacts}
|
||||||
{$Contacts["phone"]["fax"]}<br>
|
phone: {$sec1/contacts.phone}
|
||||||
{$Contacts["phone"]["home"]}<br>
|
fax: {$sec1/contacts.fax}
|
||||||
{$Contacts["phone"]["work"]}<br>
|
cell: {$sec1/contacts.cell}
|
||||||
{$Contacts["fax"]}<br>
|
{/section}
|
||||||
|
|
||||||
<p>
|
<p>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user