Jump to:

5514 Posts in 1733 Topics by 1219 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Using $ Template Variables in CMS Content area ?

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

Page: 1
Go to End
Author Topic: 366 Views
  • pinkp
    Avatar
    Community Member
    168 Posts

    Using $ Template Variables in CMS Content area ? Link to this post

    I want to use template variables in the CMS. The same way UserDefinedForms allows you to place $UserDefinedForm in the Content HTML editor field.

    This may have been covered but I cant find the correct example.

    I want to be able to add a new tab to my page such as "My Custom Code" and the information on this tab to then be placed at the users choice in their content using $MyCustomCode.
    I would also like to do this with any extra fields I create in the Site Config.

    I found this code in the UserDefinedForms but not sure how to translate it to my situation, or if this is all you need etc.

    /**
        * Using $UserDefinedForm in the Content area of the page shows
        * where the form should be rendered into. If it does not exist
        * then default back to $Form.
        *
        * @return array
        */
       public function index() {
          if($this->Content && $form = $this->Form()) {
             $hasLocation = stristr($this->Content, '$UserDefinedForm');
             if($hasLocation) {
                $content = str_ireplace('$UserDefinedForm', $form->forTemplate(), $this->Content);
                return array(
                   'Content' => DBField::create_field('HTMLText', $content),
                   'Form' => ""
                );
             }
          }

          return array(
             'Content' => DBField::create_field('HTMLText', $this->Content),
             'Form' => $this->Form()
          );
       }

    thanks

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Using $ Template Variables in CMS Content area ? Link to this post

    You can do like userforms (exactly as you found - str_replace()) or use short codes http://www.ssbits.com/tutorials/2010/2-4-using-short-codes-to-embed-a-youtube-video/

  • pinkp
    Avatar
    Community Member
    168 Posts

    Re: Using $ Template Variables in CMS Content area ? Link to this post

    Thanks Willr, just a couple more questions, could you please show me how to change it for my variable $MyVariable

    This has nothing to default back to like in the example, and what do I replace Form with. Your advice would be much appreciated.

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Using $ Template Variables in CMS Content area ? Link to this post

    Sorry, not sure I follow your problem with the code you posted? Say you want to do as UserForms does but replace $MyVariable simply remove the Form specific stuff and update the $MyVariable.

    public function index() {
    $value = "My AWESOME Value";

    if($this->Content) {
    $hasLocation = stristr($this->Content, '$MyVariable');

    if($hasLocation) {
    $content = str_ireplace('$MyVariable', $value, $this->Content);
    return array(
    'Content' => DBField::create_field('HTMLText', $content),
    );
    }
    }

    return array(
    'Content' => DBField::create_field('HTMLText', $this->Content),
    );
    }

  • pinkp
    Avatar
    Community Member
    168 Posts

    Re: Using $ Template Variables in CMS Content area ? Link to this post

    The problem was I just wasn't sure how to strip it
    OK so I added that to the Page.php and now where ever I place $MyVariable it shows "My AWSEOME Value"

    But what I want it to return is the content of a field in the CMS. So usually I show the content of a field in the template with $MyVariableContent, but I want the user to fill in the field. "this is the users content" then in the content area they place $MyVariable and this shows "this is the users content" not the $value content My AWSOME Value...

    hope that makes sense, thanks for your patience.

  • Willr
    Avatar
    Forum Moderator
    5489 Posts

    Re: Using $ Template Variables in CMS Content area ? Link to this post

    Then replace $value = "My AWESOME Value"; with $value = $this->Field;

    Where Field is the name of your database field.

  • pinkp
    Avatar
    Community Member
    168 Posts

    Re: Using $ Template Variables in CMS Content area ? Link to this post

    Exactly what I needed, thanks so much, that will be so useful. Cheers

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