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

Bug with #set inside #foreach: setting properties on loop variable from foreach doesn't work #129

Closed
ElvenMonky opened this issue Aug 31, 2020 · 1 comment · Fixed by #130

Comments

@ElvenMonky
Copy link

ElvenMonky commented Aug 31, 2020

const velocity = require('velocityjs');

const context = {
  records: [{ ID: '1' }, { ID: '2' }, { ID: '3' }],
};

const template1 = `
#foreach($item in $records)
    #set( $item.key = $item.ID )
#end
$records
`;

// This does not behave as I'd expect.
console.log(velocity.render(template1, context));

const template2 = `
#foreach($x in $records)
    #set( $item = $x )
    #set( $item.key = $item.ID )
#end
$records
`;

// This does not behave as I'd expect.
console.log(velocity.render(template2, context));

In example above template1 doesn't change items on $records and original array gets rendered.

Currently, you have to copy loop variable into another variable, like in template2, to be able to change properties on items.

This is not a duplicate of #100

@shepherdwind
Copy link
Owner

new version 2.0.1 published

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.

2 participants