Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: macro n:attr expands arrays #158

Merged
merged 1 commit into from May 17, 2017

Conversation

@dakujem
Copy link
Contributor

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

This comment has been minimized.

Copy link
Member

dg commented Apr 12, 2017

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

@dakujem

This comment has been minimized.

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
1 of 2 checks passed
1 of 2 checks passed
continuous-integration/travis-ci/pr The Travis CI build failed
Details
coverage/coveralls Coverage remained the same at 94.597%
Details
dg added 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 added 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

This comment has been minimized.

Copy link
Contributor

TomasVotruba commented May 17, 2017

Thank you @dakujem

@dg dg mentioned this pull request May 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.