Jump to:

23473 Posts in 18941 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Dynamic "content blocks" possible in SilverStripe?

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 2
Go to End
Author Topic: 3760 Views
  • abures
    Avatar
    Community Member
    3 Posts

    Re: Dynamic "content blocks" possible in SilverStripe? Link to this post

    Ok I'm pretty new to SilverStripe and came up with own solution for content blocks... I got it working and it works great for me... can someone tell me if there's a better way or if I'm breaking some sort of "best use" practices for SilverStripe... Or if this works well?

    Basically in my editor content area I can enter something like $contentblock_41_insert -- I didn't know how to carry over a variable since I want the content area to change on different pages so I embedded the page ID into the variable.. you'll see the "41" there... for page id 41. It has to be in the content area (not on a template) since my clients have to be able to move around content blocks at will. This couldn't be tied to the .ss templates.

    So basically I had previously created a page which has the page id 41... now if I create a new page... I simply put in $contentblock_41_insert somewhere on the new page and it pulls in the content from page id 41. But I can use $contentblock_1_insert or $contentblock_5_insert... whatever page exists that I want to pull into a new page and those become the content blocks on the new pages!

    Then in the controller (/mysite/code/Page.php) I put this method which is where all the magic occurs. Any advice is appreciated if I'm doing something wrong or inefficient!

    function Content() {
       $replace = $this->Content;
       $currentcontent = $replace;
       if (preg_match("/\\\$contentblock/",$currentcontent)) {
          $currentcontent_expl = explode('contentblock_',$currentcontent);
          $numberofelements = count($currentcontent_expl);
          if ($numberofelements > 1) {
             for ($currentcount = 1; $currentcount < $numberofelements; $currentcount++) {
                $contentelement = $currentcontent_expl[$currentcount];
                if (preg_match("/_insert/",$contentelement)) {
                   $subcontent_expl = explode("_insert",$contentelement);
                   $newcontent = trim($subcontent_expl[0]);
                   if ($newcontent) {
                      $pullblock_arr[] = $newcontent;
                      }
                   }
                }
             }
          if ($pullblock_arr) {
             foreach ($pullblock_arr as $val) {
                $usethis = DataObject::get_by_id("Page", $val);
                $usethiscontent = $usethis->Content;
                if (preg_match("/\\\$contentblock_".$val."_insert/",$replace)) {
                   $replace = str_replace('$contentblock_'.$val.'_insert', $usethiscontent, $replace);
                   }
                }
             }
          }
       return $replace;
       }

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Dynamic "content blocks" possible in SilverStripe? Link to this post

    This is something that you could use the shortcodes for, it would take care of the replacement for you and be a tiny bit nicer for the cms user.

    You could then add a feature to the editor to allow a user to pick a page, rather than find the page ID

  • abures
    Avatar
    Community Member
    3 Posts

    Re: Dynamic "content blocks" possible in SilverStripe? Link to this post

    Can you give me example code and how to add it to the editor? I haven't been able to find any good documentation so specific code examples would help greatly. I couldn't find a solution that's why I went the route I did but would like a cleaner solution if you have one for me!

  • Pigeon
    Avatar
    Community Member
    243 Posts
  • abures
    Avatar
    Community Member
    3 Posts

    Re: Dynamic "content blocks" possible in SilverStripe? Link to this post

    Ok here is my final solution for "Content Blocks" using short codes from the tutorial you gave me.

    I created an .ss file in /themes/sometheme/templates/Includes/ContentBlock.ss with the following code

    <!-- ContentBlockID $ContentBlockID -->
    $ContentBlockContent
    <!-- End ContentBlockID $ContentBlockID -->

    Next, I edited /mysite/code/Page.php

    class Page extends SiteTree {

       ...

       public static function ContentBlockShortCodeHandler($arguments,$caption = null,$parser = null) {
       if (empty($arguments['id'])) {
       return;
       }

       $customize = array();
       /*** SET DEFAULTS ***/
       $customize['ContentBlockID'] = $arguments['id'];
       $usethis = DataObject::get_by_id("Page", $arguments['id']);
       $usethiscontent = $usethis->Content;
       $customize['ContentBlockContent'] = $usethiscontent;
       $customize = array_merge($customize,$arguments);
       //get our ContentBlock template
       $template = new SSViewer('ContentBlock');
       //return the customized template
       return $template->process(new ArrayData($customize));
       }
    }

    Then I edited the _config.php... I added this code to /mysite/_config.php

    ShortcodeParser::get()->register('ContentBlock',array('Page','ContentBlockShortCodeHandler'));

    then in the editor to call the Content Block (which is just a regular page in SS) by it's ID I put the shortcode inside the HTML whereever I want

    [ContentBlock id=91]

    Lastly I did the /dev/build?flush=all in the URL to register everything.

    Please let me know if I'm doing something inefficiently or if this is ok? It works for me!

  • Nobrainer Web
    Avatar
    Community Member
    134 Posts

    Re: Dynamic "content blocks" possible in SilverStripe? Link to this post

    For someone who might come across this post, i just wanted to link my module - SilverStripe Sections - the module does pretty much what is asked of here. The module is for SS 3.1.2.
    https://github.com/NobrainerWeb/Silverstripe-Section-Module

    3760 Views
Page: 1 2
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.