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.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

function to print array content


Go to End
Reply


10 Posts   2970 Views

Avatar
ojalà

Community Member, 87 Posts

15 October 2008 at 3:50am

Hi!
I want to transfrom this php code in a function for Page.php to print the element of array.
My PHP function is:

<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y','z'));
print_r ($a);
?>

that I change in

function Cicle()
   {
      $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y','z'));
      return $a;

   }

Silverstripe show me the word "arry" not his cvontent. what is wrong?

Avatar
Willr

Forum Moderator, 5513 Posts

15 October 2008 at 1:28pm

The template parser can only handle strings or SilverStripe 'Objects' which for templates is something called 'ViewableData'. So in order to pass an array of data you need to wrap it in whats called ArrayData which will make it available to the template parser

function Cicle()
{
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y','z'));
return new ArrayData($a);
}

should work. Then you can do all the template controls to access it. So you would do <% control Circle %>$a<% end_control %> etc

Avatar
ojalà

Community Member, 87 Posts

15 October 2008 at 8:05pm

Hi!
thank for answer. I have an other questio Would be possible that the command <% control %> don't work? I Don't see array..and I have the same problem when I copied the code to show last news, as explained in tutorial..

Avatar
Willr

Forum Moderator, 5513 Posts

15 October 2008 at 8:17pm

sure but its highly unlikely. Make sure havent got a typo in the code. if its still doesnt work then paste of content of your page.php file

Avatar
ojalà

Community Member, 87 Posts

15 October 2008 at 8:56pm

the content of page.php is:

<?php

class Page extends SiteTree {
   static $db = array(
   "ShowInTabMenu" => "Boolean"
   );
   
   static $defaults = array(
   );

   
}

class Page_Controller extends ContentController {
   function init() {
      parent::init();
      
      Requirements::themedCSS("layout");
      Requirements::themedCSS("typography");
      Requirements::themedCSS("form");
      
      Requirements::themedCSS('themes/' . SSViewer::current_theme() . '/css/chromestyle.css');
Requirements::javascript('themes/' . SSViewer::current_theme() . '/javascript/chrome.js');

      
   }

   
   
   function Cicle()
   {
      $name="ciao";
      $a = array ('a' , 'b' , 'c');
      return new ArrayData($a);

   }
   
}

?>

and in my page.ss, in div link to content I write

<div id="Content">   <!--blocco centrale-->
   <% control Cicle %>$a<% end_control %>
$Content
</div>

Avatar
Willr

Forum Moderator, 5513 Posts

15 October 2008 at 9:11pm

Hmm looks like you are on the right track. Try change $a to

$a = array ('a' => 'This is a' , 'b' => 'This is b' , 'c' => 'This is c');

And see if $a outputs anything in your template

Avatar
ojalà

Community Member, 87 Posts

15 October 2008 at 9:22pm

Thanks, something is good, I see "This is a". But not all the $a array..

Avatar
Willr

Forum Moderator, 5513 Posts

15 October 2008 at 9:26pm

Because when you do $a in the template you are refering to the 'a' key in the array (as you are not passing the $a array, you are passing the contents of the $a array). So to access the other values in the template you need to do things like

<% control Cicle %>
$a $b $c ..
<% end_control %>

Go to Top