Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Array Data Not rendering on template


Go to End


7 Posts   3457 Views

Avatar
Lexandclo

Community Member, 55 Posts

26 February 2016 at 5:58am

HI All

I have a created a email Template which grabs the Data for a data object.
All renders fine on the EmailTemplate.ss.
But within that data is an array , so when i print_r i get

 [MyDataObjects] =>  Array   ( [47] => 47 )  

But when i try to loop through them in the template i get nothing
I have tried

<% control MyData %>
         <% loop MyDataObjects %>
                              <h3> $MyDataObjects </h3>
           <% end_loop %>
  <% end_control %>

and

<% if MyDataObjects %>
         <% loop MyDataObjects %>
                              <h3> $MyDataObjects </h3>
           <% end_loop %>
  <% end_if %>

Any ideas would be greatly received

Thanks

Darren

Avatar
martimiz

Forum Moderator, 1391 Posts

26 February 2016 at 9:25am

Silverstripe templates aren't built to render arrays - they will basically only render strings, objects or objectlists like DataObjects and DataLists. For (non-DataObject) array related data, you can use ArrayData and ArrayList, as is explained here:

https://docs.silverstripe.org/en/3.2/developer_guides/templates/rendering_templates/

Where an ArrayData object replaces an associative array, and works just like a DataObject in template. And an ArrayList can contain a set of Arrays to loop through.

Avatar
Lexandclo

Community Member, 55 Posts

27 February 2016 at 4:20am

Hi Martimiz
i Now have put in array list but still can't call in template.
this is a dump of the data in the email

[MyData] =&gt; Array
                                (
                                     =&gt; /admin/quotes/Quote/EditForm/field/Quote/item/74/ItemEditForm
                                    [FirstName] =&gt; pete
                                    [Surname] =&gt; riggs
                                    [email= =&gt; [email=riggs@aol.com]riggs@aol.com] =&gt; [email=riggs@aol.com]riggs@aol.com[/email]
                                    [Phone] =&gt; 57575757
                                    [Message] =&gt; testing contact page
                                    [EventDate] =&gt; 2016-02-19
                                    [PartyName] =&gt; 
                                    [ID] =&gt; 74
                                    [TotalOfPeople] =&gt; 
                                    [EnableGateway] =&gt; No
                                    [PartyType] =&gt; Not Set
                                    [TotalPrice] =&gt; 2630.00
                                    [MyDataID] =&gt; 39
                                    [MyDataObject] =&gt; Array
                                        (
                                            [47] =&gt; 47
                                            [48] =&gt; 48
                                            [70] =&gt; 70
                                        )

                                    [EmailToSend] =&gt; email
                                    [Details] =&gt; Array
                                        (
                                            [GridState] =&gt; {"GridFieldSortableHeader":{"SortColumn":[]},"GridFieldFilterHeader":{"Columns":[]},"GridFieldPaginator":{"currentPage":1}}
                                        )

                                    [filter] =&gt; Array
                                        (
                                            [Details] =&gt; Array
                                                (
                                                    [FirstName] =&gt; 
                                                    [Surname] =&gt; 
                                                    [Email] =&gt; 
                                                    [Phone] =&gt; 
                                                    [Status] =&gt; 
                                                )

                                        )

                                    [SecurityID] =&gt; aa0fdb7f2a267b0803c30b633bb168f36c5df1df
                                    [action_doTake] =&gt; 1
                                    [BackURL] =&
                                )

                            [MyDataObject] =&gt; ArrayList Object
                                (
                                    [items:protected] =&gt; Array
                                        (
                                            [0] =&gt; ArrayData Object
                                                (
                                                    [array:protected] =&gt; Array
                                                        (
                                                            [ActId] =&gt; 47
                                                            [Title] =&gt; MyDataObjectTitle1
                                                        )

                                                    [failover:protected] =&gt; 
                                                    [customisedObject:protected] =&gt; 
                                                    [objCache:ViewableData:private] =&gt; Array
                                                        (
                                                        )

                                                    [class] =&gt; ArrayData
                                                    [extension_instances:protected] =&gt; Array
                                                        (
                                                        )

                                                    [beforeExtendCallbacks:protected] =&gt; Array
                                                        (
                                                        )

                                                    [afterExtendCallbacks:protected] =&gt; Array
                                                        (
                                                        )

                                                )[/code]

What i need is to render the MyDataObject in the template , the Actid and Title.

when i loop through the the dataobect i.e.
[code]
                                <% control MyDataObject  %>
                                <% loop MyDataObject %>
                              <h3>  Name =  $Title </h3>
                             <% end_loop %>
                              <% end_control %>
[/code]

it displays the loop, i.e. in it will show the 'Name' the correct amount of times but not the $Title.

Any Ideas?

Many Thanks

Darren

Avatar
martimiz

Forum Moderator, 1391 Posts

27 February 2016 at 5:02am

Could you maybe just post some clear code instead of these var_dumps? Because it's all a bit confusing, for instance calling an ArrayList 'MyDataObject', where it is all but a DataObject, makes things sort of hard to read... :)

Avatar
Lexandclo

Community Member, 55 Posts

27 February 2016 at 5:09am

ok, so i managed to get it to work, but not sure if its the correct way.


                $actnid =  $data['MyDataPage']; // get the data from data object
                $actn = SiteTree::get()->byId($actnid); // get each page by the id
               
      				
				$output = new ArrayList();
			
				foreach($actnid as $act) {
					$actp = DataObject::get_by_id('MyDataPage', $act);// loop through each page 
					$output->push(new ArrayData(array(
						
						'ActId' => $act,
						'Title' => $actp->Title,
						
						
					)));
					
				}  
				
				if($this->ActId) {  
					return $output; 
				}

then in the populate email template

'MyDataObject' =>  $output,

so when i control the data WITHOUT the loop

 <% control MyDataObject  %>
                                
                              <h3> Object Name =  $Title </h3>
                            
                              <% end_control %>

this seems to work,

Avatar
martimiz

Forum Moderator, 1391 Posts

27 February 2016 at 5:47am

Edited: 27/02/2016 5:49am

Notice you are still using 2.4 syntax, while, if you're using ArrayList, you must be on version 3.x! Also, it should be far simpler. I guess $data['MyDataPage']; contains an array of page ID's? in that case you could:

$actnid =  $data['MyDataPage'];
$output = new ArrayList();
foreach($actnid as $act) {
     $actn = MyDataPage::get()->byID($act);
     $output->push($actn)
}
return $output;

or even (off the top of my head):

$actnid =  $data['MyDataPage']; 
return MyDataPage::get()->filter('ID', $actnid);  // <-- should return a DataList 

And then in your template:

<% loop $MyDataObject  %>
     <h3> Object Name =  $Title </h3>
<% end_loop %>

Avatar
Lexandclo

Community Member, 55 Posts

27 February 2016 at 5:55am

Yes, i changed all the control to With

i knew there was a simpler way.

many Thanks, the get()->filter works like a charm.

Thanks again