Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » function to print array content

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

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

Page: 1 2
Go to End
Author Topic: 2697 Views
  • ojalà
    Avatar
    Community Member
    87 Posts

    function to print array content Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: function to print array content Link to this post

    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

  • ojalà
    Avatar
    Community Member
    87 Posts

    Re: function to print array content Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: function to print array content Link to this post

    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

  • ojalà
    Avatar
    Community Member
    87 Posts

    Re: function to print array content Link to this post

    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>

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: function to print array content Link to this post

    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

  • ojalà
    Avatar
    Community Member
    87 Posts

    Re: function to print array content Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: function to print array content Link to this post

    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 %>

    2697 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.