question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cossssmincommented, Apr 6, 2021
0reactions
cossssmincommented, May 18, 2022

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:

---
articles:
  -
    title: "Article 1"
    description: ""
  -
    title: "Article 2"
    description: "description"
---

<each loop="article in page.articles">
	<component 
		src="article_preview.html" 
		title="{{ article.title }}"
		description="{{ article.description }}"
	></component>
</each>
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found