Skip to content

Conversation

@dakujem
Copy link
Contributor

@dakujem dakujem commented Apr 6, 2017

When the first and only argument to n:attr macro is an array, expand it.

  • bug fix? no
  • new feature? yes
  • BC break? yes, in a rare (and not really sensible) case

I opened another PR solving the same thing by adding a new macro here: #157

Motivation:
Suppose there is an array $attributes containing attributes for <meta> tag.

array (
  'content' => 'width=device-width, initial-scale=1',
  'name' => 'viewport',
  'http-equiv' => NULL,
  'scheme' => NULL,
)

The developer wants to achieve this HTML output:

<meta content="width=device-width, initial-scale=1" name="viewport">

However, using <meta n:attr="$meta"> the actual output is:

<meta 0="content:width=device-width, initial-scale=1 name:viewport">

The developer is currently forced to use workarounds, like:

{=Nette\Utils\Html::el('meta', $attributes)}

The solution:
The n:attr macro be changed to expand the input array in case that:

  • the macro receives an array with only a single element
  • the single element is an array itself

Code:

'$_tmp = %node.array; echo LR\Filters::htmlAttributes( isset($_tmp[0]) && is_array($_tmp[0]) && count($_tmp) === 1 ? $_tmp[0] : $_tmp);'

After the proposed change, the developer can use

<meta n:attr="$attributes">

with an expected result.

Caveat:
This change may actually lead to a changed behaviour if in an IMHO rare case someone actually wanted to output something like <tag 0="foo:bar"> passing the array directly to the macro like

<tag n:attr="['foo' => 'bar']">

Sidenote:
In this case, the macro is used for outputting metas:

<meta n:foreach="$metas as $meta" n:attr="$meta">

This approach is however very useful for many other cases during output generation.

When the first and only argument to n:attr macro is an array, expand it.
@dakujem dakujem closed this Apr 12, 2017
@dg
Copy link
Member

dg commented Apr 12, 2017

I think it is good idea to support <meta n:attr="$attributes">

@dakujem
Copy link
Contributor Author

dakujem commented May 2, 2017

You can reopen the PR if you wish.

@dg dg reopened this May 2, 2017
@dg dg merged commit 7b52d27 into nette:master May 17, 2017
dg pushed a commit that referenced this pull request May 17, 2017
When the first and only argument to n:attr macro is an array, expand it.
dg pushed a commit that referenced this pull request May 17, 2017
When the first and only argument to n:attr macro is an array, expand it.
@TomasVotruba
Copy link
Contributor

Thank you @dakujem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants