/** * Renders a single radio input and label. * * @param string|int $val The value of the radio input. * @param string|array $text The label text, or complex radio type. * @param array $data Additional options for input generation. * @param \Cake\View\Form\ContextInterface $context The form context * @return string */ protected function _renderInput($val, $text, $data, $context) { $escape = $data['escape']; if (is_int($val) && isset($text['text'], $text['value'])) { $radio = $text; $text = $radio['text']; } else { $radio = ['value' => $val, 'text' => $text]; } $radio['name'] = $data['name']; if (empty($radio['id'])) { $radio['id'] = $this->_id($radio['name'], $radio['value']); } if (isset($data['val']) && is_bool($data['val'])) { $data['val'] = $data['val'] ? 1 : 0; } if (isset($data['val']) && strval($data['val']) === strval($radio['value'])) { $radio['checked'] = true; } if ($this->_isDisabled($radio, $data['disabled'])) { $radio['disabled'] = true; } if (!empty($data['required'])) { $radio['required'] = true; } if (!empty($data['form'])) { $radio['form'] = $data['form']; } $input = $this->_templates->format('radio', ['name' => $radio['name'], 'value' => $escape ? h($radio['value']) : $radio['value'], 'attrs' => $this->_templates->formatAttributes($radio, ['name', 'value', 'text'])]); $label = $this->_renderLabel($radio, $data['label'], $input, $context, $escape); if ($label === false && strpos($this->_templates->get('radioWrapper'), '{{input}}') === false) { $label = $input; } return $this->_templates->format('radioWrapper', ['input' => $input, 'label' => $label]); }
/** * Test adding templates through the constructor. * * @return void */ public function testConstructorAdd() { $templates = ['link' => '<a href="{{url}}">{{text}}</a>']; $template = new StringTemplate($templates); $this->assertEquals($templates['link'], $template->get('link')); }