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.

Use variables outside of loop

See original GitHub issue

Hi! I have a code in edge template:

      @if(field.type == 'checkbox')
          <fieldset id="{{ field.name }}">
            @each(checkBoxItem in field.data)
              <span class="checkbox-item">
                <input type="checkbox" id="{{ checkBoxItem.name }}" name="{{ field.name }}" value="{{ checkBoxItem.value }}" {{ checkBoxItem.checked ? "checked" : "" }}>
                <label for="{{ checkBoxItem.name }}">{{ checkBoxItem.title }}</label>
              </span>
            @endeach
          </fieldset>
      @endif

When I use ‘field.name’ outside of @each loop, it works fine, but when I use it inside a loop, it renders as ‘undefined’.

P.S. Wanna say thanks to all people who are working with Adonis, Edge and all ecosystem, it’s really cool.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
thetutlagecommented, Oct 26, 2017

Ohh yeah, I believe it is not documented well, but for nested values, you make use of $parent keyword.

So it should be

$parent.field.name
1reaction
PazzaVladcommented, Oct 22, 2017

@thetutlage thanks for reply

I’ve tried your example, and it works well. Here is my full template that produce given error. I guess this issue occurred when using nested loops.

Template:

@layout('dashboard/layout')

@section('content')

<form class="forms" method="post">

  @each(field in fields)
  <div class="content-block">
    <div class="field {{ field.type }} {{ field.required ? "required" : "" }}">
    <h2 class="title"> {{ field.title }}</h2>
    <div class="description">{{ field.description }}</div>

      {{--  Text fields  --}}
      @if(field.type == 'text')
          <input type="text" name="{{ field.name }}" {{ field.required ? "required" : "" }} value="{{ field.value ? field.value : "" }}" >
      @endif

      {{--  Number fields  --}}
      @if(field.type == 'number')
          <input type="number" id="{{ field.name }}" name="{{ field.name }}" min="{{ field.min }}" max="{{ field.max }}" step="{{ field.step }}" value="{{ field.value ? field.value : "" }}" {{ field.required ? "required" : "" }} />
      @endif

      {{--  Textarea fields  --}}
      @if(field.type == 'textarea')
          <textarea name="{{ field.name }}" cols="{{ field.cols }}" rows="{{ field.rows }}" {{ field.required ? "required" : "" }}>{{ field.value ? field.value : "" }}</textarea>
      @endif

      {{--  Radio buttons fields  --}}
      @if(field.type == 'radio')
          <fieldset id="{{ field.name }}">
            @each(radioItem in field.data)
              <span class="radio-item">
                <input type="radio" id="{{ radioItem.name }}" name="{{ field.name }}" value="{{ radioItem.value }}" {{ field.checked ? "checked" : "" }} {{ field.required ? "required" : "" }}>
                <label for="{{ radioItem.name }}">{{ radioItem.title }}</label>
              </span>
            @endeach
          </fieldset>
      @endif

      {{--  Checkbox fields  --}}
      @if(field.type == 'checkbox')
          <fieldset id="{{ field.name }}">
            @each(checkBoxItem in field.data)
              <span class="checkbox-item">
                <input type="checkbox" id="{{ checkBoxItem.name }}" name="{{ field.name }}" value="{{ checkBoxItem.value }}" {{ checkBoxItem.checked ? "checked" : "" }}>
                <label for="{{ checkBoxItem.name }}">{{ checkBoxItem.title }}</label>
              </span>
            @endeach
          </fieldset>
      @endif

      {{--  Select fields  --}}
      @if(field.type == 'select')
          <select name="{{ field.name }}" id="{{ field.name }}" {{ field.required ? "required" : "" }} {{ field.multiple ? "multiple" : 0 }}> 
            @each(selectItem in field.data)
              <option value="{{ selectItem.value }}" {{ field.selected ? "selected" : "" }}>{{ selectItem.title }}</option> 
            @endeach
          </select>
      @endif

      {{--  Range fields  --}}
      @if(field.type == 'range')
          <input type="range" id="{{ field.name }}" name="{{ field.name }}" min="{{ field.min }}" max="{{ field.max }}" step="{{ field.step }}" value="{{ field.value }}" oninput="{{ field.name }}_output.value = {{ field.name }}.value" />
          <output name="{{ field.name }}_output" id="{{ field.name }}_output">{{ field.value }}</output>
      @endif

    </div>
  </div>
  @endeach

  {{ csrfField() }}
  
  <input type="submit" value="Отправить">
</form>
@endsection

Controller:

return view.render('dashboard/fields', {
      fields: 
     [
      {
        type: "radio",
        name: "gadget_type",
        title: "Тип устройства",
        description: "Выбериты тип устройства",
        required: true,
        data: [
          { name: "smartphone", title: "Смартфон", value: "smartphone" },
          { name: "phablet", title: "Фаблет", value: "phablet" },
          { name: "phone", title: "Телефон", value: "phone" },
        ]
      },
    ]
    })

output:


     <fieldset id="gadget_type">
              <span class="radio-item">
                <input type="radio" id="smartphone" name="undefined" value="smartphone"  >
                <label for="smartphone">Смартфон</label>
              </span>
              <span class="radio-item">
                <input type="radio" id="phablet" name="undefined" value="phablet"  >
                <label for="phablet">Фаблет</label>
              </span>
              <span class="radio-item">
                <input type="radio" id="phone" name="undefined" value="phone"  >
                <label for="phone">Телефон</label>
              </span>
          </fieldset>


Read more comments on GitHub >

github_iconTop Results From Across the Web

JAVA: Is it possible to use a variable outside a loop that has ...
SOLUTION: What you have to do to use a variable outside a loop, is to declare it before the loop begins, you don't...
Read more >
Define a variable inside or outside of a loop | coders corner
In case you define the variable outside of the loop in can be accessed in a bigger scope. And therefore the variable which...
Read more >
Is it important to declare a variable outside a for loop before?
“Declare two variables i and j for use with the for loops.” I know that it works without the declaration but was wondering...
Read more >
In Java, how do I use a variable that has been declared in a ...
Declare the variable outside the while loop. If you declare the variable inside the loop then its scope will be limited to the...
Read more >
How to change a variable outside of a for loop? Scoping Issues
Solved: This question is extremely common; How do I use a counter in a variable that was created outside of a for loop....
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