Skip to content

Advanced form rendering

Steve "Uru" West edited this page Jun 19, 2014 · 7 revisions

You can easily implement your own rendering method by extending the Render class and using an instance of your own Render.

Custom Input types

Any object can be rendered by making sure the class implements Fuel\Fieldset\Render\Renderable. This interface does not have any functions but acts as a safeguard so you don't accidentally pass something bad to the renderer.

<?php

class MyCustomWidget implements \Fuel\Fieldset\Render\Renderable
{
    //Regular class guts go here
}

If you where to try and render this now the generic renderInput method would get called and it would most likely not render as you would like. Creating your own renderer is simple and just requires you to extend or implement a Renderer and add a magic render method in like so:

<?php
class CustomRender extends \Fuel\Fieldset\Render\BasicRender
{
    public function renderMyCustomWidget($widget)
    {
        //Do some stuff to generate HTML
        return $myAwesomeHTML;
    }
}

Now when the render function encounters your custom object it will try and call your new method, if that does not exit it will just call the render method in the current element. This allows user created input types to be rendered with any renderer class, regardless of if they know about that input type. An additional render function can be defined, renderInput that will be used as a catch all if a specialized render method cannot be found.

Order of render method searching:

  • render<ClassName>
  • renderInput
  • <ClassName>render

If no render method can be found, for whatever reason, an exception is thrown.

Clone this wiki locally