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