0

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.

1
  • 1
    Amm... But why ? Commented Sep 23, 2016 at 8:39

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 "." :-)

7 Comments

This is not dynamic. It is still static.
@RAVI The thing is you can use this inside a loop.. that's why it can be dynamic
Nothing to do with loop. check KidBinary's answer.
@RAVI... meaning.. using that trick.. you can call a variable from a value without hardcoding the variable's name..
@jakub .. Did my answer make sense somehow?.. :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.