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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

Global $var access


Go to End


16 Posts   4477 Views

Avatar
UncleCheese

Forum Moderator, 4102 Posts

14 January 2009 at 4:00am

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;
}
}

Avatar
MrHyde

Community Member, 23 Posts

14 January 2009 at 5:38am

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

14 January 2009 at 6:13am

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

14 January 2009 at 6:19am

Actually, before you do that, try

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

Avatar
MrHyde

Community Member, 23 Posts

14 January 2009 at 6:28am

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

Avatar
Hamish

Community Member, 712 Posts

14 January 2009 at 10:28am

Edited: 14/01/2009 10:28am

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;
    }
}

Avatar
MrHyde

Community Member, 23 Posts

14 January 2009 at 10:33pm

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

Avatar
solar

10 Posts

18 January 2009 at 5:48pm

Edited: 19/01/2009 8:07am

deleted

Go to Top