Jump to:

23001 Posts in 11854 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » Global $var access

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 2
Go to End
Author Topic: 1863 Views
  • UncleCheese
    Avatar
    4085 Posts

    Re: Global $var access Link to this post

    I didn't realize this was an editable field in the CMS. Well, supposing you only have one HomePage, this should work.. Let's assume the field you're looking for on your homepage is called "GlobalText".

    class Page_Controller extends Content_Controller
    {

    function Text()
    {
    return singleton('HomePage')->GlobalText;
    }
    }

  • MrHyde
    Avatar
    Community Member
    23 Posts

    Re: Global $var access Link to this post

    Sorry, i tried your code, doesnt work for me. Any further ideas?

  • UncleCheese
    Avatar
    4085 Posts

    Re: Global $var access Link to this post

    Please post all of your code for HomePage.php, Page.php and the relevant part of Page.ss

  • UncleCheese
    Avatar
    4085 Posts

    Re: Global $var access Link to this post

    Actually, before you do that, try

    DataObject::get_one('HomePage')->GlobalText;

  • MrHyde
    Avatar
    Community Member
    23 Posts

    Re: Global $var access Link to this post

    Ok, here we go

    HomePage.php:

    class HomePage extends Page {

    static $db = array(
    'varText' => 'Varchar(25)'
    );

    function getCMSFields() {
    $fields = parent::getCMSFields();
    $fields->addFieldToTab('Root.Content.Global', new TextField('varText'), 'Content');
    return $fields;
    }
    }

    class HomePage_Controller extends Page_Controller {
    }

    Page.php:

    class Page extends SiteTree {
    static $db = array(
    );
    }

    class Page_Controller extends ContentController {
    function Text() {
    return singleton('HomePage')->varText;
    }
    }

    Page.ss

    $Text

    HF

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Global $var access Link to this post

    assuming that you only have one page defined as a 'HomePage', then this should work:

    class Page_Controller extends ContentController {
    function Text() {
    return DataObject::get_one('HomePage')->varText;
    }
    }

  • MrHyde
    Avatar
    Community Member
    23 Posts

    Re: Global $var access Link to this post

    I took yor code. Works fine now. Thanks Hamish and Uncle for your help.

  • solar
    Avatar
    10 Posts

    Re: Global $var access Link to this post

    deleted

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