Re: [SMARTY] accessing values in multi-dimensional associative arrays

From: Date: Mon, 02 May 2005 09:01:12 +0000
Subject: Re: [SMARTY] accessing values in multi-dimensional associative arrays
References: 1 2  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-12565@lists.php.net to get a copy of this message
Hi Mark, Thank you for your help. I've set up a test page using standard php (the code for which is in an attached text file - might be stripped from email?) This page uses the same array structure (just not assigned to smarty using $smarty->assign) and then prints values to screen using the following: == start code: standard php foreach loop accessing doc-scope array == foreach($doc['stylesheets']['css']['linked'] as $key => $value) { echo htmlentities('<link rel="' . $value['rel'] . '" href="' . $value['href'] . '" charset="' . $value['charset'] . '" title="' . $value['title'] . '" media="' . $value['media'] . '" />') . '<br />'; } == end code == This works perfectly. However, attempting to mimic this foreach loop in smarty fails. This is how I'm trying it (I've updated smarty config to use xml-style delimiters): == start smarty code == <smarty>foreach from="$doc.stylesheets.css.linked" key="key" item="stylesheet"</smarty> <link rel="<smarty>$stylesheet.rel</smarty>" type="text/css" href="<smarty>$stylesheet.href</smarty>" charset="<smarty>$stylesheet.charset</smarty>" title="<smarty>$stylesheet.title</smarty>" media="<smarty>$stylesheet.media</smarty>" /> <smarty>/foreach</smarty> == end smarty code == == smarty output == <link rel="A" type="text/css" href="A" charset="A" title="A" media="A" /> == end smarty output == Any ideas?! :D Thanks again for taking the time to help. I really appreciate it. Chris On Sunday 1 May 2005 12:18 pm, Mark Rogers wrote: > Looking at your example, on the first iteration of the loop > $key = 'default'; > $stylesheet = array( > 'rel' => 'stylesheet', > 'href' => 'stylesheet.css', > 'charset' => 'iso-8859-1', > 'title' => 'Default Stylesheet', > 'media' => 'screen' > ); > > So your code would be fine if you swap {$key.rel} for {$stylesheet.rel} and > likewise for all other components. > > Think of the equivalent PHP foreach code: > foreach($doc['stylesheets']['css']['linked'] as $key => > $stylesheet) { > // use $stylesheet['rel'] (etc) here, not $key['rel'] which is > meaningless > }

<?php $doc = array( 'xmlProlog' => '<?xml version="1.0" encoding="iso-8859-1"?>', 'doctype' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">', 'title' => 'smarty test 1', 'mimetype' => 'application/xhtml+xml', 'charset' => 'iso-8859-1', 'language' => 'en-GB', 'uri' => 'http://townson.local/index.php', 'lastModDate' => '2005-04-30', 'meta' => array( 'nextPage' => array( 'url' => 'http://townson.local/next.php', 'title' => 'next page' ), 'prevPage' => array( 'url' => 'http://townson.local/prev.php', 'title' => 'previous page' ), 'author' => 'Christopher Townson', 'contributors' => array( 'Christopher Townson', 'Another person' ), 'keywords' => 'accessibility usability web-standards xhtml css web-development', 'description' => 'accessibility and usability-focused, standards-compliant web design by Christopher Townson' ), 'stylesheets' => array( 'css' => array( 'linked' => array( array( 'rel' => 'stylesheet', 'href' => '/b/t/default/theme.css', 'charset' => 'iso-8859-1', 'title' => 'Default Stylesheet', 'media' => 'screen', ), array( 'rel' => 'alternate stylesheet', 'href' => 'alt.css', 'charset' => 'iso-8859-1', 'title' => 'Alternate Stylesheet', 'media' => 'screen', ), array( 'rel' => 'stylesheet', 'href' => 'print.css', 'charset' => 'iso-8859-1', 'title' => 'Print Stylesheet', 'media' => 'print', ), ), 'imported' => array( '/b/t/default/theme.css', 'alt.css', 'print.css', ), ), ), 'id' => 'home' ); foreach($doc['meta']['contributors'] as $key => $value) { echo '<p>' . $value . '</p>'; } foreach($doc['stylesheets']['css']['linked'] as $key => $value) { echo htmlentities('<link rel="' . $value['rel'] . '" href="' . $value['href'] . '" charset="' . $value['charset'] . '" title="' . $value['title'] . '" media="' . $value['media'] . '" />') . '<br />'; } echo htmlentities('<style type="text/css">') . '<br />'; foreach($doc['stylesheets']['css']['imported'] as $key => $value) { echo htmlentities('@import "' . $value . '";') . '<br />'; } echo htmlentities('</style>'); ?>
« previous php.smarty.general (#12565) next »