Page 1 of 1

[solved] Returning an array from a UDT

Posted: Wed Aug 27, 2008 11:47 am
by stopsatgreen
I have a UDT called 'string2array' which takes a comma-separated list from a comment block and explodes it into an array:

Code: Select all

global $gCms;
$myVar = $smarty->get_template_vars('blocks');
$block_array = explode(',',$myVar);
return $block_array;
I can't then work out the next step, to take the contents of this array and loop through it; I think the loop is fine:

Code: Select all

{section name='block' loop=$block-array}
Is: {$block-array[block]}<br />
{/section}
But how do I take string2array and assign the value to block-array? I've tried:

Code: Select all

{string2array assign='block-array'}
And I've tried:

Code: Select all

{capture assign='block-array'}{string2array}{/capture}
What am I missing?

Re: Returning an array from a UDT

Posted: Wed Aug 27, 2008 11:58 am
by cyberman
Maybe this can help

Code: Select all

          {content block='blocklist' oneline='true' wysiwyg='false' assign='blocklist'}
          {assign var='blocks' value=','|explode:$blocklist}
          {foreach from=$blocks item=block}
             {global_content name=$block}
          {/foreach}
I've used it to define in an additional content block which Global Content Blocks should be displayed.

Re: Returning an array from a UDT

Posted: Wed Aug 27, 2008 12:05 pm
by stopsatgreen
Thanks. It looks like that uses a plugin; which one are you using?

Re: Returning an array from a UDT

Posted: Wed Aug 27, 2008 12:31 pm
by cyberman
No, there's no new plugin :). It's only a little bit Smarty magic.

I know, it's not easy to find in Smarty manual, but you can use a lot of php commands as modifier for Smarty variables ...

Re: Returning an array from a UDT

Posted: Wed Aug 27, 2008 1:17 pm
by stopsatgreen
It's strange, I still can't get it to work. Here's my call to the content block, which is at the top of the page:

Code: Select all

{content block='Blocks' assign='block-array' oneline='true' wysiwyg='false'}
And here's where I explode the string:

Code: Select all

{assign var='blocks' value=','|explode:$block-array}
{foreach from=$blocks item=block}
{global_content name=$block}
{/foreach}
But it's just returning a value of 0.

Re: Returning an array from a UDT

Posted: Thu Aug 28, 2008 7:16 am
by stopsatgreen
After a little investigation, it seems there may be an issue with the assign='' parameter; for example, I have the following code:

Code: Select all

{content block='Blocks' assign='block-array' oneline='true'}
When I call the variable:

Code: Select all

{$block-array}
I get a value of 0, when I should be getting a string. Likewise, when I try to assign the value of my UDT to a variable:

Code: Select all

{string2array assign='myArray'}
I get a value of 0. This doesn't seem right.

Re: Returning an array from a UDT

Posted: Thu Aug 28, 2008 9:26 am
by alby
stopsatgreen wrote: After a little investigation, it seems there may be an issue with the assign='' parameter; for example, I have the following code:
Try to use:

Code: Select all

{content block='Blocks' assign='blockarray' oneline='true' wysiwyg='false'}
and

Code: Select all

{$blockarray}
Alby

Re: Returning an array from a UDT

Posted: Thu Aug 28, 2008 9:40 am
by stopsatgreen
I can't believe it was that simple!

Thanks to you both for the help.

Is there somewhere in the documentation that mentions that issue, or should I add it somewhere?

Re: [solved] Returning an array from a UDT

Posted: Thu Aug 28, 2008 7:32 pm
by cyberman