From ce59516242243fc61361b80051de3c4146e317f9 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Thu, 9 May 2019 11:17:14 -0400 Subject: [PATCH 1/2] Add htmlTagAttributes filter to Twig --- src/web/twig/Extension.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/web/twig/Extension.php b/src/web/twig/Extension.php index 956354086a3..d791b42a5f5 100644 --- a/src/web/twig/Extension.php +++ b/src/web/twig/Extension.php @@ -16,6 +16,7 @@ use craft\helpers\DateTimeHelper; use craft\helpers\Db; use craft\helpers\FileHelper; +use craft\helpers\Html; use craft\helpers\Json; use craft\helpers\Sequence; use craft\helpers\StringHelper; @@ -214,6 +215,7 @@ public function getFilters(): array new TwigFilter('filterByValue', [ArrayHelper::class, 'filterByValue']), new TwigFilter('group', [$this, 'groupFilter']), new TwigFilter('hash', [$security, 'hashData']), + new TwigFilter('htmlTagAttributes', [$this, 'htmlTagAttributesFilter']), new TwigFilter('id', [$this->view, 'formatInputId']), new TwigFilter('index', [ArrayHelper::class, 'index']), new TwigFilter('indexOf', [$this, 'indexOfFilter']), @@ -707,6 +709,17 @@ public function multisortFilter($array, $key, $direction = SORT_ASC, $sortFlag = return $array; } + /** + * Renders HTML tag attributes with [[\craft\helpers\Html::renderTagAttributes()]] + * + * @param array $attributes + * @return Markup + */ + public function htmlTagAttributesFilter(array $attributes): Markup + { + return TemplateHelper::raw(Html::renderTagAttributes($attributes)); + } + /** * @inheritdoc */ From 9bf5982afee260838f81db8247b2b7865278dab6 Mon Sep 17 00:00:00 2001 From: Tim Kelty Date: Fri, 10 May 2019 06:35:40 -0400 Subject: [PATCH 2/2] Make htmlTagAttributes a Twig fn, rename --- src/web/twig/Extension.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/web/twig/Extension.php b/src/web/twig/Extension.php index d791b42a5f5..4dedddf5d0c 100644 --- a/src/web/twig/Extension.php +++ b/src/web/twig/Extension.php @@ -215,7 +215,6 @@ public function getFilters(): array new TwigFilter('filterByValue', [ArrayHelper::class, 'filterByValue']), new TwigFilter('group', [$this, 'groupFilter']), new TwigFilter('hash', [$security, 'hashData']), - new TwigFilter('htmlTagAttributes', [$this, 'htmlTagAttributesFilter']), new TwigFilter('id', [$this->view, 'formatInputId']), new TwigFilter('index', [ArrayHelper::class, 'index']), new TwigFilter('indexOf', [$this, 'indexOfFilter']), @@ -709,17 +708,6 @@ public function multisortFilter($array, $key, $direction = SORT_ASC, $sortFlag = return $array; } - /** - * Renders HTML tag attributes with [[\craft\helpers\Html::renderTagAttributes()]] - * - * @param array $attributes - * @return Markup - */ - public function htmlTagAttributesFilter(array $attributes): Markup - { - return TemplateHelper::raw(Html::renderTagAttributes($attributes)); - } - /** * @inheritdoc */ @@ -729,6 +717,7 @@ public function getFunctions(): array new TwigFunction('alias', [Craft::class, 'getAlias']), new TwigFunction('actionInput', [$this, 'actionInputFunction']), new TwigFunction('actionUrl', [UrlHelper::class, 'actionUrl']), + new TwigFunction('attr', [$this, 'attrFunction']), new TwigFunction('cpUrl', [UrlHelper::class, 'cpUrl']), new TwigFunction('ceil', 'ceil'), new TwigFunction('className', 'get_class'), @@ -759,6 +748,17 @@ public function getFunctions(): array ]; } + /** + * Renders HTML tag attributes with [[\craft\helpers\Html::renderTagAttributes()]] + * + * @param array $attributes + * @return Markup + */ + public function attrFunction(array $config): Markup + { + return TemplateHelper::raw(Html::renderTagAttributes($config)); + } + /** * Returns a CSRF input wrapped in a \Twig\Markup object. *