jakub
jakub

Reputation: 43

Laravel: variable + parameter in view

I want to ask if it is possible to do something like this in View in Laravel 5.2:

<p> This is window: {{$element_ + 'window'}} </p> <p> This is wall: {{$element_ + 'wall'}} </p>

The values for this variables are from $element_window, $element_wall.

Upvotes: 0

Views: 116

Answers (2)

Damaged Organic
Damaged Organic

Reputation: 8467

There are couple of options.

First - is to use @php block in .blade file for dynamic output:

@php
${'window'} = ${$element_.'window'}
@endphp

Second is to write custom blade extension to output anything you need.

Third is to define custom method in your Model (if you use one).

However I should mention, that such variable assignment inside template (first option) is not recommended. It's hardly readable and could cause Exceptions if such dynamically created variables do not exist at some point. Not saying that this is not presentation logic.

Upvotes: 1

Emz
Emz

Reputation: 542

If you want to dynamically name a variable.. you can do the following.

<p> This is window: {{ ${'element_'.'window'} }} </p>
<p> This is wall: {{ ${'element_'.'wall'} }} </p>

That should work.

But if just want to concatenate a string to the variable... you can use "." :-)

Upvotes: 0

Related Questions