adrexia
7/21/2014 - 4:13 AM

Custom templates on Page_Controller to work with userforms

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