if/else condition with local variable inside component not working
See original GitHub issue- Maizzle Version: 3.3.0
- Node.js Version: 14.9.0
Sample code
<!-- template: myemail.html -->
---
articles:
-
title: "Article 1"
description: ""
-
title: "Article 2"
description: "description"
---
<each loop="article in page.articles">
<!-- this works -->
<p>{{article.title}}</p>
<p>
<if condition="article.description">has description</if>
<else>no description</else>
</p>
<!-- duplicating above inside a component -->
<component src="article_preview.html" locals='{
"title": "{{article.title}}",
"description": "{{article.description}}"
}'></component>
</each>
<!-- component: article_preview.html -->
<p>{{title}}</p> <!-- this works -->
<p>{{description}}</p> <!-- this works. It's "" (1) or "description" (2) -->
<!-- The condition is always true -->
<p>
<if condition="description">has description</if>
<else>no description</else>
</p>
<!-- This breaks the build: 'ReferenceError: article is not defined' -->
<p>
<if condition="description">{{description}}</if>
</p>
Issue 1
Using <if condition="foo">testing foo</if>
in a component, where foo is a local variable
condition="foo"
→ always true even when foo is undefined or empty string
condition="foo == undefined"
→ always false
condition="foo == 'undefined'"
→ always false
condition="foo == 'bar'"
(knowing foo is ‘bar’) → always false
Issue 2
Specific to when component is loaded inside an each loop.
Using {{foo}}
inside <if></if>
in a component
<if condition="foo">{{foo}}</if>
breaks the build
✖ Failed to compile template: myemail.html
⚠ ReferenceError: article is not defined
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Why can't variables be declared in an if statement?
Variables can be declared inside a conditional statement. However you try and access b in a different scope. When you declare b here:...
Read more >How to re-initialize the local variable which is created while ...
Hi, 1) May I know how to change or re-initialize the local variable which has been initialized in the starting of the expression...
Read more >Effective Dart: Usage
Dart offers no way to tell if a late variable has been initialized or assigned to. If you access it, it either immediately...
Read more >Variable scope - Manual
In PHP global variables must be declared global inside a function if they are ... A static variable exists only in a local...
Read more >Variable scope, closure
If a variable is declared inside a code block {. ... { // do some job with local variables that should not be...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Probably related to https://github.com/maizzle/framework/issues/397
Tested and works fine in latest alpha (
v4.0.0-alpha.8
), you’ll need to use expressions passed through attributes to the component like so: