[solved] Returning an array from a UDT

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
stopsatgreen
Power Poster
Power Poster
Posts: 322
Joined: Sat Feb 04, 2006 1:24 am
Location: London, England

[solved] Returning an array from a UDT

Post 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?
Last edited by stopsatgreen on Thu Aug 28, 2008 9:41 am, edited 1 time in total.
cyberman

Re: Returning an array from a UDT

Post 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.
stopsatgreen
Power Poster
Power Poster
Posts: 322
Joined: Sat Feb 04, 2006 1:24 am
Location: London, England

Re: Returning an array from a UDT

Post by stopsatgreen »

Thanks. It looks like that uses a plugin; which one are you using?
cyberman

Re: Returning an array from a UDT

Post 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 ...
stopsatgreen
Power Poster
Power Poster
Posts: 322
Joined: Sat Feb 04, 2006 1:24 am
Location: London, England

Re: Returning an array from a UDT

Post 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.
stopsatgreen
Power Poster
Power Poster
Posts: 322
Joined: Sat Feb 04, 2006 1:24 am
Location: London, England

Re: Returning an array from a UDT

Post 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.
alby

Re: Returning an array from a UDT

Post 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
stopsatgreen
Power Poster
Power Poster
Posts: 322
Joined: Sat Feb 04, 2006 1:24 am
Location: London, England

Re: Returning an array from a UDT

Post 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?
Post Reply

Return to “Developers Discussion”