silverstripe-framework/docs/en/02_Developer_Guides/01_Templates/04_Rendering_Templates.md
Aaron Carlino 6888901468
NEW: Update docs to be compliant with Gatsby site (#9314)
* First cut

* Temporarily disable composer.json for netlify build

* POC

* New recursive directory query, various refinements

* Fix flexbox

* new styled components plugin

* Apply frontmatter delimiters

* Mobile styles, animation

* Search

* Redesign, clean up

* Nuke the cache, try again

* fix file casing

* Remove production env file

* ID headers

* Move app to new repo

* Add frontmatter universally

* Hide children changelogs

* Add how to title

* New callout tags

* Revert inline code block change

* Replace note callouts

* Fix icons

* Repalce images

* Fix icon

* Fix image links

* Use proper SQL icon
2019-11-18 17:58:33 +13:00

3.4 KiB

title summary icon
Rendering data to a template Call and render SilverStripe templates manually. code

Rendering data to a template

Templates do nothing on their own. Rather, they are used to render a particular object. All of the <% if %>, <% loop %> and other variables are methods or parameters that are called on the current object in scope. All that is necessary is that the object is an instance of ViewableData (or one of its subclasses).

The following will render the given data into a template. Given the template:

app/templates/Coach_Message.ss

<strong>$Name</strong> is the $Role on our team.

Our application code can render into that view using renderWith. This method is called on the ViewableData instance with a template name or an array of templates to render.

app/code/Page.php

$arrayData = new SilverStripe\View\ArrayData([
    'Name' => 'John',
    'Role' => 'Head Coach'
]);

echo $arrayData->renderWith('Coach_Message');

// returns "<strong>John</strong> is the Head Coach on our team."

[info] Most classes in SilverStripe you want in your template extend ViewableData and allow you to call renderWith. This includes Controller, FormField and DataObject instances. [/info]

$controller->renderWith(['MyController', 'MyBaseController']);

SilverStripe\Security\Security::getCurrentUser()->renderWith('Member_Profile');

renderWith can be used to override the default template process. For instance, to provide an ajax version of a template.

use SilverStripe\CMS\Controllers\ContentController;

class PageController extends ContentController
{
    private static $allowed_actions = ['iwantmyajax'];

    public function iwantmyajax()
    {
        if (Director::is_ajax()) {
            return $this->renderWith('AjaxTemplate');
        } else {
            return $this->httpError(404);
        }
    }
}

Any data you want to render into the template that does not extend ViewableData should be wrapped in an object that does, such as ArrayData or ArrayList.

use SilverStripe\View\ArrayData;
use SilverStripe\ORM\ArrayList;
use SilverStripe\Control\Director;
use SilverStripe\CMS\Controllers\ContentController;

class PageController extends ContentController
{
    // ..
    public function iwantmyajax()
    {
        if (Director::is_ajax()) {
            $experience = new ArrayList();
            $experience->push(new ArrayData([
                'Title' => 'First Job'
            ]));

            return $this->customise(new ArrayData([
                'Name' => 'John',
                'Role' => 'Head Coach',
                'Experience' => $experience
            ]))->renderWith('AjaxTemplate');
        } else {
            return $this->httpError(404);
        }
    }
}