Jump to:

22970 Posts in 11572 Topics by 2823 members

General Questions

SilverStripe Forums » General Questions » Cannot access SiteConfig from module template

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: 341 Views
  • vwd
    Avatar
    Community Member
    157 Posts

    Cannot access SiteConfig from module template Link to this post

    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.

  • vwd
    Avatar
    Community Member
    157 Posts

    Re: Cannot access SiteConfig from module template Link to this post

    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.

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