Home > Archive > PHP Smarty Templates > May 2005 > Re: [SMARTY] accessing values in multi-dimensional associative arrays
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: [SMARTY] accessing values in multi-dimensional associative arrays
|
|
| Mark Rogers 2005-05-01, 9:00 am |
| Christopher Townson writes:
> == 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 ==
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']['link
ed'] as $key => $stylesheet) {
// use $stylesheet['rel'] (etc) here, not $key['rel'] which is
meaningless
}
Mark Rogers,
More Solutions Ltd
| |
| Christopher Townson 2005-05-02, 8:58 am |
| 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']['link
ed'] 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']['link
ed'] as $key => $stylesheet) {
> // use $stylesheet['rel'] (etc) here, not $key['rel'] which is
> meaningless
> }
| |
| Mark Rogers 2005-05-03, 4:01 pm |
| Christopher Townson writes:
> 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 ==
I haven't had chance to look through this in full, but it looks to me like
the problem is the quotes around the from value in the foreach:
<smarty>foreach from="$doc.stylesheets.css.linked" key="key"
should be
<smarty>foreach from=$doc.stylesheets.css.linked key="key"
or (not recommended, but equivalent)
<smarty>foreach from="`$doc.stylesheets.css.linked`" key="key"
Mark Rogers,
More Solutions Ltd
|
|
|
|
|