accessing values in multi-dimensional associative arrays

From: Date: Sun, 01 May 2005 07:00:51 +0000
Subject: accessing values in multi-dimensional associative arrays
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-12560@lists.php.net to get a copy of this message
Hi - Having put it off for a long time, I've started experimenting with Smarty (2.6.9) and it's excellent! However, I've been having a bit of difficulty working out the syntax required in {foreach} and {section} loops to access multi-dimensional, associative arrays. The situation: I'm designing my templates to access values in a series of arrays which detail document-scope and site-scope properties. The idea is that, using this method, an array can be built by my application from any (changing) source (database, xml file etc) without the templates being affected in any way (i.e. MVC). An example of the document-scope array that I am feeding to the templates is at the end of this message. The problem: I would like to add a sub-array for stylesheets to enable to the loading of individual stylesheets for specific documents and/or server-side style-switching. An example of the way I wrote this array was (it's inside the $doc array) is at the bottom of this message. However, because there can be more than stylesheet (and the array key is a unique identifier for each stylesheet), the stylesheet values cannot simply be accessed using $doc.stylesheets.css.linked.... and so on. The question: How do I loop through each of the stylsheets listed in the array (linked.default, linked.alternate, linked.print, imported.extraRules) inserting the values for rel, href and so on into the document html for each? Right at the bottom of the email, I have put my really feeble attempt at doing this for you everyone to have a laugh at! ;D Thanks in advance for any help and suggestions, Chris == start code: example doc-scope array == $smarty->assign('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 10', 'mimetype' => 'application/xhtml+xml', 'charset' => 'iso-8859-1', 'language' => 'en', 'uri' => 'http://domain.com/thisPage.php', 'lastModDate' => $lastModDate, 'meta' => array( 'nextPage' => array( 'url' => 'http://doamin.com/nextPage.php', 'title' => 'next page' ), 'prevPage' => array( 'url' => 'http://domain.com/prevPage', 'title' => 'previous page' ), 'author' => 'Christopher Townson', 'contributors' => array( 'Christopher Townson', 'Another Person' ), 'keywords' => 'keyword keyword keyword keyword keyword', 'description' => 'description of page contents', ), 'id' => 'home' ) ); == end code == == start code: example doc-scope array with stylsheet array == $smarty->assign('doc',array( 'stylesheets' => array( 'css' => array( 'linked' => array( 'default' => array( 'rel' => 'stylesheet', 'href' => 'stylesheet.css', 'charset' => 'iso-8859-1', 'title' => 'Default Stylesheet', 'media' => 'screen', ), 'alternate' => array( 'rel' => 'alternate stylesheet', 'href' => 'alt_stylesheet.css', 'charset' => 'iso-8859-1', 'title' => 'Alternate Stylesheet', 'media' => 'screen', ), 'print' => array( 'rel' => 'stylesheet', 'href' => 'print.css', 'charset' => 'iso-8859-1', 'title' => 'Print Stylesheet', 'media' => 'print', ), ), 'imported' => array( 'extraRules' => array( 'rel' => 'stylesheet', 'href' => 'stylesheet_add.css', 'charset' => 'iso-8859-1', 'title' => 'Default Stylesheet', 'media' => 'screen', ), ), ), ), ) ); == end code == == start code: my feeble attempt at looping though stylesheet values! == {foreach from="$doc.stylesheets.css.linked" key="key" item="stylesheet"} <link rel="{$key.rel}" type="text/css" href="{$key.href}" charset="{$key.charset}" title="{$key.title}" media="{$key.media}" /> {/foreach} == end code ==

« previous php.smarty.general (#12560) next »