lumeland / lume

🔥 Static site generator for Deno 🦕
https://lume.land
MIT License
1.75k stars 74 forks source link

Plugin Nav: Cannot access 'item' before initialization #577

Closed dzeroc closed 4 months ago

dzeroc commented 4 months ago

Version

2.1.0

Platform

Ubuntu 23.10, Deno 1.41

What steps will reproduce the bug?

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

No error.

What do you see instead?

{{ include "./menu.vto" }}

Error in the template /_includes/menu.vto:4:7

{{ include "menu_item.vto" { item } }}

Error in the template /_includes/menu_item.vto:10:3

{{ for item of item.children }}

Cannot access 'item' before initialization

at Environment.createError (https://deno.land/x/vento@v0.10.2/src/environment.ts:277:12)
at eval (eval at compile (https://deno.land/x/vento@v0.10.2/src/environment.ts:42:25), <anonymous>:24:23)
at eventLoopTick (ext:core/01_core.js:153:7)
at async Environment.runString (https://deno.land/x/vento@v0.10.2/src/environment.ts:87:14)
at async VentoEngine.render (https://deno.land/x/lume@v2.1.0/plugins/vento.ts:94:20)
at async Renderer.render (https://deno.land/x/lume@v2.1.0/core/renderer.ts:216:19)
at async Renderer.#renderLayout (https://deno.land/x/lume@v2.1.0/core/renderer.ts:293:17)
at async https://deno.land/x/lume@v2.1.0/core/renderer.ts:159:28
at async Promise.all (index 0)
at async concurrent (https://deno.land/x/lume@v2.1.0/core/utils/concurrent.ts:21:3)

Caused by Error: Error in the template /_includes/menu.vto:4:7

{{ include "menu_item.vto" { item } }}

Error in the template /_includes/menu_item.vto:10:3

{{ for item of item.children }}

Cannot access 'item' before initialization

at Environment.createError (https://deno.land/x/vento@v0.10.2/src/environment.ts:277:12)
at eval (eval at compile (https://deno.land/x/vento@v0.10.2/src/environment.ts:42:25), <anonymous>:26:23)
at eventLoopTick (ext:core/01_core.js:153:7)
at async Environment.run (https://deno.land/x/vento@v0.10.2/src/environment.ts:69:12)
at async eval (eval at compile (https://deno.land/x/vento@v0.10.2/src/environment.ts:42:25), <anonymous>:11:19)
at async Environment.runString (https://deno.land/x/vento@v0.10.2/src/environment.ts:87:14)
at async VentoEngine.render (https://deno.land/x/lume@v2.1.0/plugins/vento.ts:94:20)
at async Renderer.render (https://deno.land/x/lume@v2.1.0/core/renderer.ts:216:19)
at async Renderer.#renderLayout (https://deno.land/x/lume@v2.1.0/core/renderer.ts:293:17)
at async https://deno.land/x/lume@v2.1.0/core/renderer.ts:159:28

Caused by Error: Error in the template /_includes/menu_item.vto:10:3

{{ for item of item.children }}

Cannot access 'item' before initialization

at Environment.createError (https://deno.land/x/vento@v0.10.2/src/environment.ts:277:12)
at eval (eval at compile (https://deno.land/x/vento@v0.10.2/src/environment.ts:42:25), <anonymous>:43:23)
at Environment.run (https://deno.land/x/vento@v0.10.2/src/environment.ts:69:18)
at eventLoopTick (ext:core/01_core.js:153:7)
at async eval (eval at compile (https://deno.land/x/vento@v0.10.2/src/environment.ts:42:25), <anonymous>:14:19)
at async Environment.run (https://deno.land/x/vento@v0.10.2/src/environment.ts:69:12)
at async eval (eval at compile (https://deno.land/x/vento@v0.10.2/src/environment.ts:42:25), <anonymous>:11:19)
at async Environment.runString (https://deno.land/x/vento@v0.10.2/src/environment.ts:87:14)
at async VentoEngine.render (https://deno.land/x/lume@v2.1.0/plugins/vento.ts:94:20)
at async Renderer.render (https://deno.land/x/lume@v2.1.0/core/renderer.ts:216:19)

Caused by ReferenceError: Cannot access 'item' before initialization at eval (eval at compile (https://deno.land/x/vento@v0.10.2/src/environment.ts:42:25), :27:41) at Environment.run (https://deno.land/x/vento@v0.10.2/src/environment.ts:69:18) at eventLoopTick (ext:core/01_core.js:153:7) at async eval (eval at compile (https://deno.land/x/vento@v0.10.2/src/environment.ts:42:25), :14:19) at async Environment.run (https://deno.land/x/vento@v0.10.2/src/environment.ts:69:12) at async eval (eval at compile (https://deno.land/x/vento@v0.10.2/src/environment.ts:42:25), :11:19) at async Environment.runString (https://deno.land/x/vento@v0.10.2/src/environment.ts:87:14) at async VentoEngine.render (https://deno.land/x/lume@v2.1.0/plugins/vento.ts:94:20) at async Renderer.render (https://deno.land/x/lume@v2.1.0/core/renderer.ts:216:19) at async Renderer.#renderLayout (https://deno.land/x/lume@v2.1.0/core/renderer.ts:293:17)

Additional information

Am I doing something wrong? I just followed the official documentation.

oscarotero commented 4 months ago

I think the example is wrong. Can you try changing menu_item.vto to this?

{{ if item.data }}
  <a href="{{ item.data.url }}">
    {{ item.data.title }}
  </a>
{{ else }}
  <span>{{ item.slug }}</span>
{{ /if }}

<ul>
  {{ for child of item.children }}
  <li>
    {{ include "templates/menu_item.vto" { item: child } }}
  </li>
  {{ /for }}
</ul>

(Rename the second item to child to avoid variable name conflict). Probably it's a bug in Vento.

dzeroc commented 4 months ago

It works! I imagined something like this, but I don't know enough about Vento. Thank you very much.

Could you please update the documentation?

oscarotero commented 4 months ago

Done! Thank you!