WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.52k stars 4.2k forks source link

Docs: Interactivity API - Invalid HTML output of wp-each and wp-each-child snippets #64417

Open atachibana opened 3 months ago

atachibana commented 3 months ago

Description

wp-each documentation says: https://github.com/WordPress/gutenberg/blob/trunk/docs/reference-guides/interactivity-api/api-reference.md

It would generate the following output:

<ul data-wp-context='{ "list": [ "hello", "hola", "olá" ] }'>
  <li data-wp-text="context.item">hello</li>
  <li data-wp-text="context.item">hola</li>
  <li data-wp-text="context.item">olá</li>
</ul>

But, output includes data-wp-each-child. 20240810_wp-each-1 Should we modify above output as followings?

<ul data-wp-context='{ "list": [ "hello", "hola", "olá" ] }'>
  <li data-wp-each-child data-wp-text="context.item">hello</li>
  <li data-wp-each-child data-wp-text="context.item">hola</li>
  <li data-wp-each-child data-wp-text="context.item">olá</li>
</ul>

Also, the wp-each-child section lists the processed results:

<ul data-wp-context='{ "list": [ "hello", "hola", "olá" ] }'>
  <template data-wp-each--greeting="context.list" >
    <li data-wp-text="context.greeting"></li>
  </template>
  <li data-wp-each-child>hello</li>
  <li data-wp-each-child>hola</li>
  <li data-wp-each-child>olá</li>
</ul>

But, it contains a <template> tag that must disappear after the processing. This entire snippet of wp-each-child should be removed, and refer to the above wp-each output.

Step-by-step reproduction instructions

  1. npx @wordpress/create-block@latest my-first-interactive-block --template @wordpress/create-block-interactive-template
  2. Replace render.php by following code:
    <h1>wp-each test</h1>
    <div data-wp-interactive="myPlugin">
    <ul data-wp-context='{ "list": [ "hello", "hola", "olá" ] }'>
    <template data-wp-each="context.list" >
    <li data-wp-text="context.item"></li>
    </template>
    </ul>
    </div>

Screenshots, screen recording, code snippet

No response

Environment info

Please confirm that you have searched existing issues in the repo.

Please confirm that you have tested with all plugins deactivated except Gutenberg.

luisherranz commented 3 months ago

Hmm... it is true that in the case of data-wp-each, saying what it generates is not clear because what it generates on the server is not exactly what it generates on the client after processing. As you mentioned, the template tag is removed from the DOM, and the data-wp-each-child directives are not removed, but are also not produced when new elements are added to the array, as they are only for identifying the elements added on the server.

So, in this case, I am not quite sure what to show without explaining the purpose of data-wp-each-child.