Jump to:

23483 Posts in 18976 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Reading contents of files

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 622 Views
  • aragonne
    Community Member
    26 Posts

    Reading contents of files Link to this post

    Hi there,

    I have an existing website that generates a footer based on the titles of the pages in the footer. The pages in the footer are not related by hierarchy in any way so I can't use the Menu() template control. Let's the say the footer looks like:


    I know I can use the Page control in the template like:

    <% control Page('home') %>$Title<% end_control %> | .... | <% control Page('employee') %>$Title<% end_control %>

    but I'd rather hide the logic in the class so I can output the footer like this:


    What do you recommend? How can I access the contents (eg, title, menu title) of pages within a class using a method in the controller:

    method Footer() {
    $footer = '';
    $footer_files = array('home', ...., 'employee');
    foreach ($footer_files as $file) {
    // read the contents of each file here;
    $footer = ...?
    return $footer;

    (as background info, the site is currently running on MODx where we used the Ditto and WayFinder snippet to generate the footer. It's basically just a file content reader that you pass a list of page ids to that you want to read content of fields from. We want to be able to duplicate this functionality in SilverStripe)


  • Willr
    Forum Moderator
    5508 Posts

    Re: Reading contents of files Link to this post

    Welcome to the forums!.

    I guess if you were going to use <% control Page() %> then the urls would have to be the same so what you can do is

    function Footer() {
    $urls = array('page-1', 'page-2', 'page-3'); // in the order you need;

    $output = new DataObjectSet();

    foreach($urls as $url) {
    $page = DataObject::get_one('Page', "URLSegment = '$url');

    if($page) $output->push($page);

    return $output;

    // in the template
    <% control Footer %>
    <a href="$Link">$MenuTitle</a>
    <% end_control %>

    Hope that helps!. Another option you have.

  • aragonne
    Community Member
    26 Posts

    Re: Reading contents of files Link to this post

    Thanks so much for the welcome and speedy response Wilr! That is exactly what I was looking for.

    I don't think this next question is possible because of the code base and also because it violates separation of logic from display, but I'll ask anyways. Is it possible to actually pass the page url into the Footer method from the template? (this type of functionality is also used elsewhere besides the footer and I'd like to have this one 'generic' method that can be shared). If not, I can make this method as generic as possible, eg, call it contentReader(array), then have methods like Footer( array('page1', 'page2', etc.) ) and Header( array('page3', 'page4', 'page5') ) that will call contentReader().

    thanks again!

Page: 1
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.