Cake\View\StringTemplate::format PHP Method

format() public method

Format a template string with $data
public format ( string $name, array $data ) : string | null
$name string The template name.
$data array The data to insert.
return string | null Formatted string or null if template not found.
    public function format($name, array $data)
    {
        if (!isset($this->_compiled[$name])) {
            throw new RuntimeException("Cannot find template named '{$name}'.");
        }
        list($template, $placeholders) = $this->_compiled[$name];
        if (isset($data['templateVars'])) {
            $data += $data['templateVars'];
            unset($data['templateVars']);
        }
        $replace = [];
        foreach ($placeholders as $placeholder) {
            $replacement = isset($data[$placeholder]) ? $data[$placeholder] : null;
            if (is_array($replacement)) {
                $replacement = implode('', $replacement);
            }
            $replace[] = $replacement;
        }
        return vsprintf($template, $replace);
    }

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::format