Cake\View\StringTemplate::formatAttributes PHP Method

formatAttributes() public method

- '1' (string) - 1 (integer) - true (boolean) - 'true' (string) Then the value will be reset to be identical with key's name. If the value is not one of these 4, the parameter is not output. 'escape' is a special option in that it controls the conversion of attributes to their HTML-entity encoded equivalents. Set to false to disable HTML-encoding. If value for any option key is set to null or false, that option will be excluded from output. This method uses the 'attribute' and 'compactAttribute' templates. Each of these templates uses the name and value variables. You can modify these templates to change how attributes are formatted.
public formatAttributes ( array | null $options, array | null $exclude = null ) : string
$options array | null Array of options.
$exclude array | null Array of options to be excluded, the options here will not be part of the return.
return string Composed attributes.
    public function formatAttributes($options, $exclude = null)
    {
        $insertBefore = ' ';
        $options = (array) $options + ['escape' => true];
        if (!is_array($exclude)) {
            $exclude = [];
        }
        $exclude = ['escape' => true, 'idPrefix' => true, 'templateVars' => true] + array_flip($exclude);
        $escape = $options['escape'];
        $attributes = [];
        foreach ($options as $key => $value) {
            if (!isset($exclude[$key]) && $value !== false && $value !== null) {
                $attributes[] = $this->_formatAttribute($key, $value, $escape);
            }
        }
        $out = trim(implode(' ', $attributes));
        return $out ? $insertBefore . $out : '';
    }

Usage Example

 /**
  * Render a text widget or other simple widget like email/tel/number.
  *
  * This method accepts a number of keys:
  *
  * - `name` The name attribute.
  * - `val` The value attribute.
  * - `escape` Set to false to disable escaping on all attributes.
  *
  * Any other keys provided in $data will be converted into HTML attributes.
  *
  * @param array $data The data to build an input with.
  * @param \Cake\View\Form\ContextInterface $context The current form context.
  * @return string
  */
 public function render(array $data, ContextInterface $context)
 {
     $data += ['name' => '', 'val' => null, 'type' => 'text', 'escape' => true];
     $data['value'] = $data['val'];
     unset($data['val']);
     return $this->_templates->format('input', ['name' => $data['name'], 'type' => $data['type'], 'attrs' => $this->_templates->formatAttributes($data, ['name', 'type'])]);
 }
All Usage Examples Of Cake\View\StringTemplate::formatAttributes