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

each fails with variable named "offerings" #3447

Open
fpereira1 opened this issue Oct 20, 2024 · 0 comments · May be fixed by #3448
Open

each fails with variable named "offerings" #3447

fpereira1 opened this issue Oct 20, 2024 · 0 comments · May be fixed by #3448

Comments

@fpereira1
Copy link

fpereira1 commented Oct 20, 2024

Pug Version: 3.0.3

Node Version: 18.12.1

Input JavaScript Values

import pug from 'pug';

const offerings = {
  offering01: {
    title: 'Offering 01',
    description: 'This is the first offering.',
    price: 100,
  },
  offering02: {
    title: 'Offering 02',
    description: 'This is the second offering.',
    price: 200,
  }
};

const pugTemplate = `
p Offerings

each offering, key in offerings
  h2= offering.title
  p= offering.description
  p= offering.price
  a(href='https://example.com/offering/' + key) More info
`;

console.log(
  pug.render(
    pugTemplate,
    {offerings}
  )
);

Additional Comments

This issue seems related to or possible the same as this other closed issue: #3263

It would be easy to convert the structure from an object to an array as a workaround, but pug should handle both

The code above fails with the following error:

   var err = new Error(fullMessage);
              ^

Error: Pug:4:32
    2| p Offerings
    3|
  > 4| each offering, key in offerings
--------------------------------------^
    5|   h2= offering.title
    6|   p= offering.description
    7|   p= offering.price

The value variable for each must either be a valid identifier (e.g. `item`) or a pair of identifiers in square brackets (e.g. `[key, value]`).
    at makeError (/pug_bug_of/node_modules/pug-error/lib/index.js:34:15)
    at Lexer.error (/pug_bug_of/node_modules/pug-lexer/index.js:62:15)
    at Lexer.eachOf (/pug_bug_of/node_modules/pug-lexer/index.js:1108:14)
    at Lexer.callLexerFunction (/pug_bug_of/node_modules/pug-lexer/index.js:1647:23)
    at Lexer.advance (/pug_bug_of/node_modules/pug-lexer/index.js:1676:12)
    at Lexer.callLexerFunction (/pug_bug_of/node_modules/pug-lexer/index.js:1647:23)
    at Lexer.getTokens (/pug_bug_of/node_modules/pug-lexer/index.js:1706:12)
    at lex (/pug_bug_of/node_modules/pug-lexer/index.js:12:42)
    at Object.lex (/pug_bug_of/node_modules/pug/lib/index.js:104:9)
    at Function.loadString [as string] (/pug_bug_of/node_modules/pug-load/index.js:53:24) {
  code: 'PUG:MALFORMED_EACH_OF_LVAL',
  msg: 'The value variable for each must either be a valid identifier (e.g. `item`) or a pair of identifiers in square brackets (e.g. `[key, value]`).',
  line: 4,
  column: 32,
  filename: undefined,
  src: '\n' +
    'p Offerings\n' +
    '\n' +
    'each offering, key in offerings\n' +
    '  h2= offering.title\n' +
    '  p= offering.description\n' +
    '  p= offering.price\n' +
    "  a(href='https://example.com/offering/' + key) More info\n",
  toJSON: [Function (anonymous)]
@fpereira1 fpereira1 linked a pull request Oct 20, 2024 that will close this issue
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 a pull request may close this issue.

1 participant