Skip to content

Instantly share code, notes, and snippets.

@adrexia
Last active August 29, 2015 14:04
Show Gist options
  • Save adrexia/9b50a191fca04bd739b0 to your computer and use it in GitHub Desktop.
Save adrexia/9b50a191fca04bd739b0 to your computer and use it in GitHub Desktop.
Custom templates on Page_Controller to work with userforms
private static $allowed_actions = array (
	'expandPageContent' 
);


/*
 * Method to retrive the rendered page template, sans any external elements (such as header, footer, nav)
 * 
 * @return HTMLText
 */
public function expandPageContent(SS_HTTPRequest $request){
	return $this->customise(
		new ArrayData($this->index($request))) //to work with userforms
		->renderWith(array("{$this->ClassName}_Content", 'Page_Content', 'Page'));
}


// if there is no index method on this object:
		
/**
 * 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->hasMethod('Form')) { 
		return $this;
	}
	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()
	);
}


Page_Content.ss:

	$Layout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment