tannerdolby / eleventy-plugin-metagen

Eleventy shortcode that generates document metadata
https://www.npmjs.com/package/eleventy-plugin-metagen
MIT License
41 stars 3 forks source link

Liquid template error #26

Closed xplosionmind closed 5 months ago

xplosionmind commented 5 months ago

Hello!

I am testing this plugin inside a Liquid template, but I get a syntax error.

This is what I wrote in the template:

{% metagen title=title or site.title, desc='An eleventy shortcode for generating meta tags.' %}

This is the error log:

$ ELEVENTY_ENV=development eleventy --serve --watch --incremental
[11ty] Problem writing Eleventy templates: (more in DEBUG output)
[11ty] 1. Having trouble rendering liquid template ./content/Everyone-it.html (via TemplateContentRenderError)
[11ty] 2. invalid syntax at line 1 col 6:
[11ty] 
[11ty] 1  title=title or site.title,
[11ty]         ^
[11ty] 2        description='An eleventy shortcode for generating meta tags.',, file:/home/tommi/dev/dev.tommi.space/includes/head.html, line:7, col:2 (via RenderError)
[11ty] 3. invalid syntax at line 1 col 6:
[11ty] 
[11ty] 1  title=title or site.title,
[11ty]         ^
[11ty] 2        description='An eleventy shortcode for generating meta tags.', (via Error)
[11ty] 
[11ty] Original error stack trace: Error: invalid syntax at line 1 col 6:
[11ty] 
[11ty] 1  title=title or site.title,
[11ty]         ^
[11ty] 2        description='An eleventy shortcode for generating meta tags.',
[11ty]     at Lexer._token (/home/tommi/dev/dev.tommi.space/node_modules/.pnpm/moo@0.5.2/node_modules/moo/moo.js:559:17)
[11ty]     at Lexer.next (/home/tommi/dev/dev.tommi.space/node_modules/.pnpm/moo@0.5.2/node_modules/moo/moo.js:506:19)
[11ty]     at Liquid.parseArguments (/home/tommi/dev/dev.tommi.space/node_modules/.pnpm/@11ty+eleventy@2.0.1/node_modules/@11ty/eleventy/src/Engines/Liquid.js:146:21)
[11ty]     at Tag.render (/home/tommi/dev/dev.tommi.space/node_modules/.pnpm/@11ty+eleventy@2.0.1/node_modules/@11ty/eleventy/src/Engines/Liquid.js:162:32)
[11ty]     at render.next (<anonymous>)
[11ty]     at toPromise (/home/tommi/dev/dev.tommi.space/node_modules/.pnpm/liquidjs@10.14.0/node_modules/liquidjs/dist/liquid.node.cjs.js:517:32)
[11ty]     at toPromise (/home/tommi/dev/dev.tommi.space/node_modules/.pnpm/liquidjs@10.14.0/node_modules/liquidjs/dist/liquid.node.cjs.js:523:25)
[11ty]     at async toPromise (/home/tommi/dev/dev.tommi.space/node_modules/.pnpm/liquidjs@10.14.0/node_modules/liquidjs/dist/liquid.node.cjs.js:525:25)
[11ty]     at async toPromise (/home/tommi/dev/dev.tommi.space/node_modules/.pnpm/liquidjs@10.14.0/node_modules/liquidjs/dist/liquid.node.cjs.js:525:25)
[11ty]     at async toPromise (/home/tommi/dev/dev.tommi.space/node_modules/.pnpm/liquidjs@10.14.0/node_modules/liquidjs/dist/liquid.node.cjs.js:525:25)
[11ty] Copied 263 files / Wrote 0 files in 2.49 seconds (v2.0.1)
error: script "start" exited with code 1

I am using Eleventy v2.0.1 (Liquid 10.14.0), with eleventy-plugin-metagen v1.8.3.

tannerdolby commented 5 months ago

Hi @xplosionmind

Thank you for bringing this to my attention. Unfortunately after further investigation and testing, this functionality is only available in Nunjucks templates 😢

I updated the eleventy-plugin-metagen docs to reflect this. The problem with Liquid templates is that their shortcode syntax doesn't use name=value pair arguments and due to the variability of arguments that metagen supports, it wouldn't be feasible to change the metagen logic for Liquid shortcodes as there are 44 supported arguments which don't follow any particular ordering.

That being said, Liquid shortcodes cannot handle {% user firstName or "fallback", lastName or "fallback" %} template logic like a shortocde within a Nunjucks template could so that is a quick answer to your original question about the syntax error. If you want to have this template logic functionality, you will have to use the metagen plugin in a Nunjucks template or use the short-hand Liquid shortcode syntax like {% metagen metadata %} where the metadata object handles any conditional logic or specific data requirements from frontmatter or some other data source like a global data file etc.

Let me know if this clears things up and I'm happy to answer any follow-up questions if needed.