phpsymfony-1.4php-5.3

symfony render a template/partial in a form


I am trying to render a template in a form using the _toString() method. The problem I am facing is that the form does not inherit from controller and I can not do $str.= $this->renderPartial('template',array('form' => $this)); How can this be achieved ?

I am using symfony 1.4 with PHP 5.3


Solution

  • You can create a widget for it:

    class fdWidgetFromPartial extends sfWidgetForm
    {
      public function __construct($options = array(), $attributes = array())
      {
        parent::__construct($options, $attributes);
    
        sfProjectConfiguration::getActive()->loadHelpers('Partial');
      }
    
      protected function configure($options = array(), $attributes = array())
      {
        $this->addRequiredOption('template');
        $this->addOption('variables', array());
      }
    
      public function render($name, $value = null, $attributes = array(), $errors = array())
      {
        return get_partial($this->getOption('template'), array_merge(array(
          'name' => $name,
          'value' => $value,
          'attributes' => $attributes,
          'errors' => $errors,
        ), $this->getOption('variables')));
      }
    }
    

    And use it like this:

    $this->setWidget('field', new WidgetFromPartial(array(
      'template' => 'modeule/template',
      'variables' => array(
        'some_data' => $this->getObject()->getData(),
      )
    )));
    

    You should NOT use any input field in the partial.