Skip to content

Conversation

@dakujem
Copy link
Contributor

@dakujem dakujem commented Apr 6, 2017

The macro expands an array of attributes, similarly to n:attr.

  • bug fix? no
  • new feature? yes
  • BC break? no
  • doc PR: nette/docs#???

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:
If attributes for a tag are present in an array, it would be simple to create the tag with an array-expanding macro:

<meta n:attr-expand="$attributes">

The macro itself is trivial:

'echo LR\Filters::htmlAttributes( %node.word );'

compred to n:attr:

'echo LR\Filters::htmlAttributes( %node.array );'

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

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

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

The macro expands an array of attributes, similarly to n:attr.
@matej21
Copy link
Contributor

matej21 commented Apr 6, 2017

you can use n:attr="(expand) $attributes" :)

@dakujem
Copy link
Contributor Author

dakujem commented Apr 6, 2017

Can you please point me towards an entry in documentation about this? I seem not to be able to find it.

@dakujem
Copy link
Contributor Author

dakujem commented Apr 12, 2017

Okay, so I finally found the reference myself (by pulling nette/docs repo and searching in source)...
The expand operator is mentioned in a completely unrelated section of quite unrelated chapter about MVC and Presenters generally: https://doc.nette.org/en/2.4/presenters#toc-links-in-templates

I do believe that this operator should be mentioned in the general Latte syntax description and have it's own description with the Latte macros, because that is where people search for macros and other features of Latte.

@matej21
Copy link
Contributor

matej21 commented Apr 12, 2017

@dakujem you are right. will you please send a PR to the doc?

@TomasVotruba
Copy link
Contributor

@matej21 Where exactly do you think this should be put in? Link to the file would be best.

@dg
Copy link
Member

dg commented May 17, 2017

Closed by #158

@dg dg closed this May 17, 2017
@TomasVotruba
Copy link
Contributor

Awesome, thanks @dg

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.

4 participants