function smarty_function_checkbox(array $params, Smarty_Internal_Template $template) { $isSwitch = isset($params['isSwitch']) ? (bool) $params['isSwitch'] : false; $checked = isset($params['checked']) ? (bool) $params['checked'] : false; $id = isset($params['id']) ? (string) $params['id'] : null; $label = (string) $params['label']; $name = isset($params['name']) ? (string) $params['name'] : null; $tabindex = isset($params['tabindex']) ? (int) $params['tabindex'] : null; $value = isset($params['value']) ? (string) $params['value'] : null; $classList = []; if (isset($params['class'])) { $classList[] = (string) $params['class']; } if ($isSwitch) { $classList[] = 'checkbox-switch'; } if (null === $id) { $id = uniqid(); } $html = smarty_function_tag(['el' => 'input', 'type' => 'checkbox', 'id' => $id, 'class' => !empty($classList) ? implode(' ', $classList) : null, 'checked' => $checked ? 'checked' : null, 'name' => $name, 'tabindex' => $tabindex, 'value' => $value], $template); $htmlLabelContent = ''; if ($isSwitch) { $htmlLabelContent .= smarty_function_tag(['el' => 'span', 'class' => 'handle'], $template); } $htmlLabelContent .= smarty_function_tag(['el' => 'span', 'content' => $label, 'class' => 'label'], $template); $html .= smarty_function_tag(['el' => 'label', 'content' => $htmlLabelContent, 'for' => $id], $template); return $html; }
function smarty_block_ratioKeeper($params, $content, Smarty_Internal_Template $template, $open) { if ($open) { return ''; } else { $ratio = 100; if (isset($params['ratio'])) { $ratio = $params['ratio'] * 100; } $width = null; if (isset($params['width']) && isset($params['height'])) { $width = (int) $params['width']; $height = (int) $params['height']; $ratio = (int) ($height / $width * 100); } $output = '<div class="ratioKeeper">'; $output .= '<div class="ratioKeeper-ratio" style="padding-bottom: ' . $ratio . '%;' . ($width ? ' width: ' . $width . 'px;' : '') . ' "></div>'; $contentAttrs = isset($params['contentAttrs']) ? $params['contentAttrs'] : []; if (isset($contentAttrs['class'])) { $contentAttrs['class'] .= ' ratioKeeper-content'; } else { $contentAttrs['class'] = 'ratioKeeper-content'; } $contentAttrs['el'] = 'div'; $contentAttrs['content'] = $content; $output .= smarty_function_tag($contentAttrs, $template); $output .= '</div>'; return $output; } }
public function testRender() { $smarty = new Smarty(); $template = $smarty->createTemplate('string:'); $this->assertContainsAll(['div', 'data-foo="3"', 'data-bar="baz"', 'foo bar'], smarty_function_tag(['el' => 'div', 'content' => 'foo bar', 'data' => ['foo' => 3, 'bar' => 'baz']], $template)); $exception = $this->catchException(function () use($template) { smarty_function_tag(['content' => 'foo bar'], $template); }); $this->assertInstanceOf('ErrorException', $exception); $this->assertContains('Param `el` missing.', $exception->getMessage()); $exception = $this->catchException(function () use($template) { smarty_function_tag(['el' => 'span', 'data' => 'foo bar'], $template); }); $this->assertInstanceOf('ErrorException', $exception); $this->assertContains('Param `data` should be an array.', $exception->getMessage()); }
function smarty_function_link(array $params, Smarty_Internal_Template $template) { $label = ''; if (isset($params['label'])) { $label = $params['label']; } unset($params['label']); $class = 'link'; if (isset($params['class'])) { $class .= ' ' . $params['class']; } unset($params['class']); $title = null; if (isset($params['title'])) { $title = $params['title']; } unset($params['title']); $icon = null; if (isset($params['icon'])) { $icon = $params['icon']; } unset($params['icon']); $iconPosition = 'left'; if (isset($params['iconPosition']) && $params['iconPosition'] === 'right') { $iconPosition = 'right'; } unset($params['iconPosition']); $data = array(); if (isset($params['data'])) { $data = (array) $params['data']; } unset($params['data']); $onclick = null; if (isset($params['onclick'])) { $onclick = $params['onclick']; } unset($params['onclick']); $href = 'javascript:;'; if (isset($params['href'])) { $href = (string) $params['href']; } unset($params['href']); $target = null; if (isset($params['target'])) { $target = (string) $params['target']; } unset($params['target']); if (isset($params['page'])) { $href = smarty_function_linkUrl($params, $template); } if (empty($label) && empty($icon) && empty($title) && 0 !== strpos($href, 'javascript:')) { $label = $href; } $iconMarkup = null; if (null !== $icon) { $iconMarkup = '<span class="icon icon-' . $icon . '"></span>'; } $html = ''; if (null !== $iconMarkup && 'left' === $iconPosition) { $html .= $iconMarkup; $class .= ' hasIcon'; } if (!empty($label)) { $html .= '<span class="label">' . CM_Util::htmlspecialchars($label) . '</span>'; $class .= ' hasLabel'; } if (null !== $iconMarkup && 'right' === $iconPosition) { $html .= $iconMarkup; $class .= ' hasIconRight'; } $attributeList = ['el' => 'a', 'content' => $html, 'href' => $href, 'class' => $class, 'title' => $title, 'onclick' => $onclick, 'target' => $target]; foreach ($data as $name => $value) { $attributeList['data-' . $name] = $value; } return smarty_function_tag($attributeList, $template); }