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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Dynamic "content blocks" possible in SilverStripe?


Go to End


14 Posts   7583 Views

Avatar
abures

Community Member, 3 Posts

29 July 2013 at 4:52pm

Edited: 29/07/2013 4:55pm

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; 
	}

Avatar
dhensby

Community Member, 253 Posts

29 July 2013 at 6:14pm

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

Avatar
abures

Community Member, 3 Posts

29 July 2013 at 6:20pm

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!

Avatar
abures

Community Member, 3 Posts

30 July 2013 at 12:18pm

Edited: 31/07/2013 7:12am

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!

Avatar
Nobrainer Web

Community Member, 138 Posts

21 January 2014 at 7:52am

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

Go to Top