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.

General Questions /

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

Cannot access SiteConfig from module template


Reply


2 Posts   441 Views

Avatar
vwd

Community Member, 160 Posts

28 October 2011 at 4:28pm

Hi,

I've created a module which outputs a particular text file. I also have some fields that I have added to SiteConfig (which works fine and can be used from page templates).

However, I'm having trouble getting this module's template to use the SiteConfig data...

Here is my code:

mytextfile/_config.php

<?php
// adds a rule to make www.site.com/myfile.txt work
Director::addRules(10, array(
   'myfile.txt' => 'MyTextFileDownload',
));

// add the extension to SiteConfig
Object::add_extension('SiteConfig', 'TextFileExtrasDecorator');
?>

mytextfile/code/MyTextFileDownload.php

class MyTextFileDownload. extends Controller{

function index($url) {

return array();
}

}

mytextfile/code/TextFileExtrasDecorator.php

<?php
class TextFileExtrasDecorator extends DataObjectDecorator {

function extraStatics() {
return array(
'db' => array(
'AuthorName' => 'Varchar',
)
);
}

public function updateCMSFields(FieldSet $fields) {
$fields->addFieldToTab("Root.ExampleTextFileDetails", new TextField("AuthorName", "Author"));
}
}
?>

mytextfile/templates/MyTextFileDownload.ss

Here is my example text file.

The author is: $SiteConfig.AuthorName

This is the end of the file.

When I access this file (eg. www.site.com/myfile.txt) I am able to download the file, but what I get is:

Here is my example text file.

The author is:

This is the end of the file.

Interestingly, I have no trouble accessing $SiteConfig.AuthorName from any normal page template.

Any ideas?

Thanks very much.
VWD.

Avatar
vwd

Community Member, 160 Posts

28 October 2011 at 5:37pm

I got it to work by adding the following function to the controller... But is this the right thing to do?

   function getSiteConfig() {
      return SiteConfig::current_site_config();
   }

I notice that in the Page_Controller there are some tests for alternate SiteConfigs - is it necessary to account for this?

Thanks...

VWD.