Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I'm using Laravel and Blade on a small project and am trying to dynamically assign class names to generated elements in a for loop. However, the generated elements are generating literally, such as "<div class='form_q' .$i>".

@for($i = 0; $i < count($formdata['form2']); $i++)
  <div class='form_q'.$i>
    HTML::attributes('class')
    <div class='q'.$i.'-header'>
      <h1>{{ Form::label($formdata['form2']['q'.($i + 1)]['question'], $formdata['form2']['q'.($i + 1)]['type'], array('class' => 'btn btn-primary'))}}</h1>
    </div>
  </div>
@endfor

What's the proper syntax for concatenating a string and variable and assigning it to an class/attribute? Alternatively, what is the proper Blade syntax for generating a "div" element with an assigned class?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

You need to tell Blade or PHP you want to print the output.

Ether surround the $i variables like {{ $i }} or like <?php echo $i ?>. Blade just converts back to PHP for example, {{ }} is converted into <?php echo ?>. Keep in mind that you're still using PHP when making Blade templates.

@for($i = 0; $i < count($formdata['form2']); $i++)
  <div class='form_q{{ $i }}'>
      HTML::attributes('class')
      <div class='q{{ $i }}-header'>
        <h1>{{ Form::label($formdata['form2']['q'.($i + 1)]['question'], $formdata['form2']['q'.($i + 1)]['type'], array('class' => 'btn btn-primary'))}}</h1>
      </div>
  </div>
@endfor

You should go over the Blade docs again. http://laravel.com/docs/templates

share|improve this answer

I don't know laravel or blade, but I think you should use double curly brackets to echo a php variable, like this:

<div class='form_q'{{$i}}>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.