breachofmind / grenade

A templating library for node/express, influenced by Laravel's Blade
1 stars 0 forks source link

For and Foreach loops not working #2

Open millansingh opened 6 years ago

millansingh commented 6 years ago

I followed the docs in setting up both a for and a foreach loop. Both give me the same error:

/Users/millan.singh/Personal Projects/bootpress/node_modules/grenade/src/support/utils.js:62 var parts = args.trim().split(" in ",2); ^

TypeError: Cannot read property 'trim' of undefined at Object.parseForEach (/Users/millan.singh/Personal Projects/bootpress/node_modules/grenade/src/support/utils.js:62:26) at TemplateTag.parse (/Users/millan.singh/Personal Projects/bootpress/node_modules/grenade/src/tags/foreach.js:9:22) at new TemplateTag (/Users/millan.singh/Personal Projects/bootpress/node_modules/grenade/src/TemplateTag.js:38:40) at Parser.getTagScopes (/Users/millan.singh/Personal Projects/bootpress/node_modules/grenade/src/Parser.js:138:27) at Parser.parseTags (/Users/millan.singh/Personal Projects/bootpress/node_modules/grenade/src/Parser.js:91:21) at new Template (/Users/millan.singh/Personal Projects/bootpress/node_modules/grenade/src/Template.js:34:33) at new TemplateTag (/Users/millan.singh/Personal Projects/bootpress/node_modules/grenade/src/TemplateTag.js:34:19) at Parser.getTagScopes (/Users/millan.singh/Personal Projects/bootpress/node_modules/grenade/src/Parser.js:138:27) at Parser.parseTags (/Users/millan.singh/Personal Projects/bootpress/node_modules/grenade/src/Parser.js:91:21) at new Template (/Users/millan.singh/Personal Projects/bootpress/node_modules/grenade/src/Template.js:34:33) at Compiler.template (/Users/millan.singh/Personal Projects/bootpress/node_modules/grenade/src/Compiler.js:142:16) at Compiler. (/Users/millan.singh/Personal Projects/bootpress/node_modules/grenade/src/Compiler.js:235:33) at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)

This is my frag file:

@extends(/layouts/master)

@section(header)
    <h1>${= data.data.heading }</h1>
    <h2>${= data.data.subHeading }</h2>
@endsection

@section(body-content)
    @verbatim
    @for (var i=0; i < data.data.body.length; i++)
        <p>This is a content block</p>
        @if (data.data.body[i].component = "")
            ${= block.content }
        @endif
    @endfor
    @endverbatim

    @foreach (block in data.data.body)

    @endforeach

@endsection

The data in question is an array of objects. Each object has two keys (component and content). For the Foreach loop, I also tried putting a var in before 'block'.

Side question: is there a way that I can set this up so that I don't need to include 'data.' before my variables I pass in. IE, ${= data.heading} instead of ${= data.data.heading}. The data I send to the view has a data key in it, which makes it extra confusing, I know.

And extra aside: Thank you so much for making this package. I come from a Laravel background, and there is no JS templating engine that comes even remotely close to Blade.

EDIT: Also, if you think of something you want to do to improve this repo, you should open up a project on it and put some cards on there. I'd be happy to help.