Skip to content

Controller Action Rendering

tedkulp edited this page Apr 28, 2011 · 1 revision

In order to make rendering the proper content as flexible as possible, Silk has a number of different options to turn content into a rendered page. These are listed in order of precedence as to which takes effect given a particular request.

Controller Method Output

If the controller echos any output on it's own, no view template is rendered. This is very important, and may cause some headaches if this isn't realized. If even a rogue var_dump is in the controller's action method, it will cause the controller to skip the view template phase entirely and treat that echo'd text as it's output. It will, however, use the layout if expected and render the content in the correct place ($content) accordingly.

In this case, the simple example would be:

class TestController extends \silk\action\Controller
{
  function index()
  {
    echo "Hello World!";
  }
}

Even if the view file (eg. views/test/index.html.tpl) does exist, it will not be rendered because there was some output from the action method (index()). This is an intentional design feature.

Now, there may be some situations where the echoing of text makes sense for, say, javascript requests, but not for html requests. There is a convenience method for handling this situation.

class TestController extends \silk\action\Controller
{
  function index()
  {
    $this->renderAs('js', function() {
      echo "alert('here');";
    });
  }
}

In this case, we only echo content if this is a javascript request. Otherwise, the controller will look for the appropriate view (index.html.tpl, index.tpl, etc) and render that accordingly. Also, note that we wrapped what we sent back to the renderAs method in a Closure. We do this so that we don't waste time rendering data unless we absolutely have to -- in this case, if it's a javascript request. The Closure is only rendered when the extension actually matches.

This means you can have an example like the following, and only process as much output content as needed:

class TestController extends \silk\action\Controller
{
  function index()
  {
    $this->renderAs('html', function() {
      echo "<h1>Hello World!</h1>";
    });
    $this->renderAs('js', function() {
      echo "alert('here');";
    });
  }
}

View Template Output

This is the traditional MVC approach to handling views. The controller does whatever processing is needed, and then sets it's variables for the view to process and display.

class TestController extends \silk\action\Controller
{
  function index()
  {
    $this->set('some_variable', 'Hello World!');
    $this->setByRef('some_result', db()->complicatedQueryAndResultingArray());
  }
}

When the index method returns, the controller will look for the appropriate view to render and do so. By default, Silk has two rendering engines enabled. These are controlled in the config file and can be disabled at will. The defaults are the Smarty template rendering engine and the standard PHP file engine.

If this were an HTML request to the index action, then the following four files would be checked to see if they exist in the following order:

  • index.html.tpl
  • index.tpl
  • index.html.php
  • index.php

Whichever one was found first would be rendered, inset into the layout (if applicable), and rendering back to the browser. If no view template is found, then a 404 error is returned.

Missing Action Method

If there is an action that requires no real data or processing, then the action method in the controller can be skipped. If there is no action method, then the controller will look for the appropriate view template (as described above) and rendered directly. Just like above, if no template is found, then it will return a 404 error.