Using PHP variables in an HTML element class with Blade - php

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?

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

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

{{ $i }} = <?php echo $i ?>
Example for image BLADE with Array php
{!! Html::image( asset('/profile/'.$u->foto), 'Profile', array('class' => 'avatar;')) !!}

Related

setting variables inside a Laravel foreach loop

I've just started using the framework. In plain PHP after the opening foreach I would then set the variables then close the php tag but then from what I can work out you have to then do the Laravel #foreach tags and then open and close #php. Is there a way around this as it seems like a lot of extra work and code?
#foreach($steps as $row)
#php
$title = $row->title;
$text = $row->text;
$i = 1;
#endphp
<div class="steps-item grid-wrap">
<div class="number"
#if($text || $title)
<div class="text-wrap">
#if($title)
<h2>{{$title}}</h2>
#endif
{!! $text !!}
</div>
#php
$i++;
#endphp
#endif
</div>{{--END steps-item--}}
#endforeach
Since blade is no PHP, you have to return to PHP with that directive. But you can set/use the variables without doing that in your case:
#foreach($steps as $i => $row)
<div class="steps-item grid-wrap">
<div class="number"
#if($text || $title)
<div class="text-wrap">
#if($title)
<h2>{{ $row->title }}</h2>
#endif
{!! $row->text !!}
</div>
#php
$i++;
#endphp
#endif
</div>{{--END steps-item--}}
#endforeach
If you still want to set variables, there's a Laravel package called alexdover/blade-set. But as #brombeer pointed out, in most cases it's highly recommended to set all necessary variables in the controller before passing them to the view.
Use laravel provided loop variables:
$loop->iteration The current loop iteration (starts at 1).
It will increment in every loop iteration automatically.
e.g:
First iteration = $loop->iteration => 1 ;
Second iteration = $loop->iteration => 2 ;
so on until loop ends.
Check docs:
The Loop Variables
You can use a #for directive with sizeof($steps) like that:
#for($i=0; $i<= sizeof($steps)-1; $i++)
#endfor
#foreach ($steps as $row)
<div class="steps-item grid-wrap">
<div class="number">
<div class="text-wrap">
#if ($row->title != '')
<h2>{{$row->title}}</h2>
/* if you want to display another title when its blank you can
use if-else here otherwise not need to use if conditions for
title and text */
#endif
#if ($row->text != '')
{!! $row->text !!}
#endif
</div>
</div>
</div>
#endforeach
{{--
for an example you have $steps values like
$steps =
Array(
[0] -> 1,
[1] -> 'title',
[2] -> 'text'
);
if you want this array key value pair you have to use #foreach like
#foreach ($steps as $key=>$value)
#endforeach
you can use key value in #foreach loop only
--}}

transform collection into a list in blade

anyone know how i transform this into a list in blade
[{"applicant": "Juan"}, {"applicant": "Pedro"}]
something like:
-Juan
-Pedro
currently that collection is an item of an array
{{$ applicant [$ i]}} contains [{"applicant": "Juan"}, {"applicant": "Pedro"}]
blade file
<td>
{{$applicant[$i]}}
</td>
Controller
for ($i=0; $i <= 59; $i++) {
$materias[$i]->name;
$applicant[$i] = DB::table('projections')
->where('s1',$materias[$i]->name)
->orWhere('s2',$materias[$i]->name)
->orWhere('s3',$materias[$i]->name)
->orWhere('s4',$materias[$i]->name)
->orWhere('s5',$materias[$i]->name)
->orWhere('s6',$materias[$i]->name)
->orWhere('s7',$materias[$i]->name)
->orWhere('s8',$materias[$i]->name)
->select('applicant')->get();
}
Result
image whit the result of the view
In your Blade you need to make something like that :
Check this documentation about loop in blade
<td>
<ul>
#foreach($applicant[$i] as $appli)
<li> {{ $appli }} </li>
#endforeach
</ul>
</td>

Pagination Link

What is the correct way to call the "Links" function after this "Foreach"?
I don't know how to handle the variable to put in function.
#inject('usuarios', 'App\User')
#foreach($usuarios->getIndicados() as $user)
#endforeach
<div class="row">
<div class="col-12 text-center">
{{ $usuarios->getIndicados()->links() }}
</div>
</div>
Maybe it's just an editing error, but in your output the -tags don't seem to be closed again. Also, there should be no space like < a> at the beginning of the tag. And < a hr_ef= ... is obviously wrong.
In order to style them, you can add a class attribute to the tags while building the string and do the style-stuff in css.
This is what laravel document provides. You need to add links in the collection.
<div class="container">
#foreach ($users as $user)
{{ $user->name }}
#endforeach
{{ $users->links() }}

Laravel blade can't print double curly brackets [duplicate]

This question already has answers here:
Blade engine: print triple curly braces
(5 answers)
Closed 4 years ago.
I want to print double curly brackets from a Laravel blade template. Here is what I've tried so far and failed:
#{{ n }}
{!! '{{ n }}' }}
#php echo '{{ n }}'; #endphp
#php echo '{{ n }}'; #endphp
Here is the view I have:
<div class="new-page">
<div class="row">
<div class="col-sm-4"></div>
<h3 class="col-md-6 center">Page #if(isset($n)) {{ $n }} #else #{{ n }} #endif</h3>
</div>
</div>
Weirdly, when I try any of the above there is no error it just displays an empty page.
I am using Laravel 5.6, I've also tried googling but no solution worked.
It turns out the default app.js file that comes with Laravel is throwing an error and it is breaking the page - I don't know exactly why though. The error is as follows:
n is not defined
Page <?php if(isset($n)) echo "{{ ".$n." }}"; else echo "{{ ".n." }}"; ?>
This will work quite fine you can check below screenshots
Try once using php tag like
<?php if(isset($n)) echo $n; else echo "{{ n }}"; ?>
Can you not do something like:
#php echo "{{" . {{ $n }} . "}}" #endphp
The question being is n a variable passed in or created by a controller or declared earlier in the view?

Declaring variables in blade template

I have defined variable in blade template but it is not working. I am not getting any value of i. The column is blank.
<thead style='background-color:silver'><tr><td>S.N.</td><td>Name</td><td>Telephone No.</td><td>Mobile No.</td><td width="24%">Options</td></tr></thead>
<?php $i=1;?>
#foreach ($lists as $li)
<tr><td><?php $i++;?></td><td>{{$li->Name}}</td><td>{{$li->Telephone}}</td><td>{{$li->mobile}}</td>
<td><button>View details</button>
<button>Edit</button>
<button>Delete</button></td></tr>
#endforeach
{!! $lists->render() !!}
This code will definitely work:
<?php $i = 1; ?>
....
<?php $i++; ?>
....
{{-- This will output 2 --}}
{{ $i }}

Categories