Jump to:

23453 Posts in 18902 Topics by 2877 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: 617 Views
  • aragonne
    Avatar
    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:

    HOME | CONTACT US | DIRECTORY | LOCATIONS | RESOURCES | ABOUT US | EMPLOYEE AREA

    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:

    $Footer

    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)

    thanks!
    Steve

  • Willr
    Avatar
    Forum Moderator
    5497 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
    Avatar
    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!
    Steve

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