mafortis
mafortis

Reputation: 7138

How to print variable inside variable in laravel blade

I have unusual situation which I have to print dynamic variables inside another variable in blade but not sure if it's possible or how to?

Example

// this returns content of my template which has dynamic data in it
{!! $data['template']->content !!}

// inside that template content I have to get user name like
{{$data['customer']->name}}

the problem is printing customer name

here is sample result

one

Note: $data['customer'] is provided in view page but the problem is how to get it inside my template data.

two

Code

My code is basically is just sample code i shared above (it's all happening in blade)

{!! $data['template']->content!!}

// inside `content` there is like:

 <h4>{{$data['customer']-&gt;customer_lastname}}</h4>

Question

How can I get {{$data['customer']}} printed inside {!! $data['template']->content!!}?

Upvotes: 0

Views: 3462

Answers (1)

user3532758
user3532758

Reputation: 2271

Per my comments, you could treat and compile the template string with pre-defined patterns. For example, for customer names you could do this when constructing content text:

<h4>:customer_name</h4>

And then before sending the data to blade:

$templateContent = $data['template']->content;
$templateContent = preg_replace("/:customer_name/i", $data['customer']->name, $templateContent);
return view('yourview', compact('templateContent'));

And, in blade:

{!! $templateContent !!} 

Upvotes: 1

Related Questions