Re: [SMARTY] Assembling variables in the template

From: Date: Wed, 19 Feb 2003 21:13:41 +0000
Subject: Re: [SMARTY] Assembling variables in the template
References: 1 2  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-7933@lists.php.net to get a copy of this message
Thanks a lot Monte: Yes I agree that my data structure is too complex for the template, cause the output is also a bit complex. The resolving of the template statement: {$test_results.$sessions_sorted[sess].$key.$res_showfields[rsf]} would do the trick for me, but it looks like I can't use another template variable as a key to the associative array. I'll try another approach though: completely generating all the result rows in php and sending that array to the template. That should do it. Thanks for sharing you thoughts! -dmitri P.S.: I'll use 'fookey' variable somewhere in the code. It's
       really a good one ;-)
Monte Ohrt wrote:
Wow, this is one is complicated... multiply nested sections, math equations, assembling key names, etc. Although it is probably possible to get Smarty to parse this stuff, I think the root of your problem resides in the data structure... it forces a lot of complexity in the templates. Try to keep the template tasks simple, focus strictly on presentation instead of computation. For instance, do the math in PHP before hand and assign it instead of calculating it in the template. Having to assemble keynames in the template can probably be avoided with a better template-friendly structure. Do the work in your application to assign a structure that the template can simply loop through and display with minimal effort, and minimal knowledge of the data itself. One way to approach this is to assign your data exactly like you want it looped through and displayed. Example, if you want the output: foobar1 foo bar foo2 bar2 foo3 bar3 foobar2 foo bar foo2 bar2 foo3 bar3 foobar3 foo bar foo2 bar2 foo3 bar3 Then assign the data like this: $data = array(
       'foobar1' => array(
           'foo' => 'bar',
           'foo2' => 'bar2',
           'foo3' => 'bar3'
       ),
       'foobar2' => array(
           'foo' => 'bar',
           'foo2' => 'bar2',
           'foo3' => 'bar3'
       ),
       'foobar3' => array(
           'foo' => 'bar',
           'foo2' => 'bar2',
           'foo3' => 'bar3'
       )
     );
Then your template can loop though and display it without knowledge of the content being necessary: {foreach from=$data key=datakey item=dataval} {$datakey} {foreach from=$dataval key=$fookey item=$fooval}
      {$fookey} {$fooval}
{/foreach} {/foreach} ("fookey" is a great var name isn't it?) Typically you will use {section} for numbered arrays of data, and {foreach} with associative arrays, but either can be used depending on how it is organized and how you want to display it. Also, I'm unsure about keys beginning with a number... (1-4-2-1), that might be part of some problems you're having. I know I didn't lay out an explicit answer for you, but hope this helps from a conceptual standpoint :-) Monte On Wed, 2003-02-19 at 11:35, Dmitri Zakharov wrote:
Hi all, I have quite a bit of {section} statements in my template. Everything goes smoothly untill: (@see template snippet at the end of this message) {* POINT 1 *}. Here I am trying to generate $key variable. Looks like value attrubute of the assign custom function can be a string, not something composed of other template variables. What I get here is '---' as opposed to what I expect ex.: '1-4-2-3'. Is there any other way to do that? {* POINT 2 *} Here I am trying to retrieve values from the $test_results array, which has the following structure: $test_results = array( 'sess_23' => array(
    '1-4-2-1' => array( // this is a key from POINT 1
      'avg_rate'     => 999.99,
      'elapsed_time' => 999,
      'cpu_load'     => 99,
    ),
    '1-4-2-2' => array(
      'avg_rate'     => 888.88,
      'elapsed_time' => 888,
      'cpu_load'     => 88,
    ),
    ...
), 'sess_48' => array(
    ...
), ... ); Again Smarty doesn't like: {$test_results.$sessions_sorted[sess].$key.$res_showfields[rsf]} Is there any work around? Or maybe I am doing somethin wrong? Any help is appreciated! ;o) - dmitri {* Test results rows *} {section name=cg loop=$client_groups} {section name=rs loop=$reqsizes}
    {section name=tg loop=$targets}
      {section name=rn loop=$runs}
        <tr bgcolor="{#commonRowsColor#}">
          <td>{$client_groups[cg]}</td>
          <td>{math equation="r * t" r=$reqsizes[rs]
                                     t=$targets[tg]}</td>
          <td>{$targets[tg]}</td>
          <td>{$runs[rn]}</td>
          {* POINT 1 *}
          {assign var=key value=
             "$client_groups[cg]-$reqsizes[rs]-$targets[tg]-$runs[rn]"}
          {* show selected result fields for each session *}
          {section name=sess loop=$sessions_sorted}
            {section name=rsf loop=$res_showfields}
              <td>
        {* POINT 2 *}
        {$test_results.$sessions_sorted[sess].$key.$res_showfields[rsf]}
              </td>
            {/section}
          {/section}
          {* end of show selected result fields for each session *}
        </tr>
      {/section}
    {/section}
{/section} {/section} {* End test results rows *}


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