Jump to:

23360 Posts in 18097 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » User Editable Variable Data on Both Content and 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: 720 Views
  • scottsloane
    Avatar
    Community Member
    7 Posts

    User Editable Variable Data on Both Content and Template Link to this post

    Hi Forum,

    I am just getting started with SilverStripe. I have decided to try to put all new clients on SilverStripe due to the ease of access for the clients.

    I have worked through a little bit of the system, but I have a problem I must solve. Here is the premise of the problem. My client needs to have some content display both on a page and in the template. And this data must be editable in one place by the client. To be specific this information is their store hours. As these change over time it would be a real pain for them to have to call me every time they need it changed. And for their convenience I think it would be nice is they could edit it in one place and it display in both locations, the page and the hours page.

    Here is the issue. I am so new to silverstripe that I am not even sure what to search for. I have tried a few searches and think I understand how to get strreplace to work with the content. But that is as far as I have been able to find. If someone with a little more experience than myself could help point me in the right direction it would be really appreciated.

    Thanks everyone

  • scottsloane
    Avatar
    Community Member
    7 Posts

    Re: User Editable Variable Data on Both Content and Template Link to this post

    Sorry about the double post, my double click abilities are amazing. I deleted the duplicate.

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: User Editable Variable Data on Both Content and Template Link to this post

    You could create a DataObject for StoreHours, then build a tab for editing the store hours somewhere in the CMS and then create a function in Page.php that renders a $StoreHours variable in your template.

  • Willr
    Avatar
    Forum Moderator
    5483 Posts

    Re: User Editable Variable Data on Both Content and Template Link to this post

    Or use the SiteConfig class to make a single text field on the site dashboard which you can then use $SiteConfig.OpeningHours. If you want them to also be able to include that in the content field of the database you may want to investigate making a short code for it.

    SSbits has a couple good tutorials on both those features:

    http://ssbits.com/tutorials/2010/2-4-working-with-siteconfig/
    http://ssbits.com/tutorials/2010/2-4-using-short-codes-to-embed-a-youtube-video/

  • scottsloane
    Avatar
    Community Member
    7 Posts

    Re: User Editable Variable Data on Both Content and Template Link to this post

    Thank you everybody for the advice.

    I really like the idea of using SiteConfig as it places the CMS fields in a really easy location for the CMS users.

    However, it appears that I don't know what I am doing. I followed the SSBits tutorial http://ssbits.com/tutorials/2010/2-4-working-with-siteconfig/, I placed the CustomSiteConfig file in mysite/code and edited mysite/_config.php. However, now I am getting a 500 error when trying to load the site or rebuild the database. I am sure this is just because I have no idea what I am doing. If anyone has run into this, I would love to know why. I am going to continue to attempt to solve it.

  • scottsloane
    Avatar
    Community Member
    7 Posts

    Re: User Editable Variable Data on Both Content and Template Link to this post

    Alright, I have got it all working. I followed the SSBits tutorials. There was an added requirement to allow the store hours to change at daylight savings time. So here was my solution:

    CustomSiteConfig.php

    <?php

    class CustomSiteConfig extends DataObjectDecorator {

    function extraStatics() {
    return array(
    'db' => array(
    'MonSatHours' => 'Text',
    'MonSatDSTHours' => 'Text',
    'SunHours' => 'Text',
    'SunDSTHours' => 'Text'
    )
    );
    }

    public function updateCMSFields(FieldSet &$fields) {

    $fields->addFieldToTab("Root.Main", new TextField("MonSatHours", _t('SiteConfig.MonSatHours',"Mon-Sat Hours")));
    $fields->addFieldToTab("Root.Main", new TextField("MonSatDSTHours", _t('SiteConfig.MonSatDSTHours', "Mon-Sat DST Hours")));
    $fields->addFieldToTab("Root.Main", new TextField("SunHours", _t('SiteConfig.SunHours', "Sunday Hours")));
    $fields->addFieldToTab("Root.Main", new TextField("SunDSTHours", _t('SiteConfig.SunDSTHours', "Sunday DST Hours")));
    }

    public function getMondaySaturdayHours(){
    if(date("I"))
          return $this->owner->MonSatDSTHours;
       else
          return $this->owner->MonSatHours;
    }

    public function getSundayHours(){
        if(date("I"))
          return $this->owner->SunDSTHours;
       else
          return $this->owner->SunHours;
    }
    }

    Page.php

    <?php
    class Page extends SiteTree {

       public static $db = array(
       );

       public static $has_one = array(
       );

          
       public static function HoursShortCodeHandler($arguments, $nontent = null, $parser = null){
          if (!$arguments['type']){
             return;
          }
          $SiteConfig = DataObject::get_one('SiteConfig', "");
          $customise = array();
          if($arguments['type'] == 'MonSat'){
             if(Date('I'))
                $customise['hours'] = $SiteConfig->MonSatDSTHours;
             else
                $customise['hours'] = $SiteConfig->MonSatHours;
          }else if($arguments['type'] == 'Sun'){
             if(Date('I'))
                $customise['hours'] = $SiteConfig->SunDSTHours;
             else
                $customise['hours'] = $SiteConfig->SunHours;
          }
          
          $template = new SSViewer('Hours');
          
          return $template->process(new ArrayData($customise));
       }

    }
    ...

    Hours.ss

    $hours

    _config.php

    ...

    DataObject::add_extension('SiteConfig', 'CustomSiteConfig');

    ShortcodeParser::get()->register('Hours',array('Page','HoursShortCodeHandler'));

    And there you have it. Thank you everyone for the help in figuring this out.

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