0

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

6
  • stackoverflow.com/questions/50138850/… Commented Jul 27, 2020 at 3:00
  • @user3532758 file_get_contents probably i don't know how to use your referred link, would you mind share an answer? Commented Jul 27, 2020 at 3:04
  • I hoped to give you an idea from that link. I understood from your question that the $data['customer'] is already stored in the template content string. Laravel will treat it as a string. If you want to print such variables you would need to resolve to using low level functions like eval. But if you are open to changing your methods, you could treat the content string before sending to blade; use pattern matching to replace predefined patterns. Commented Jul 27, 2020 at 3:10
  • @user3532758 I am open to change my code in anyway that could print the result. that $data['customer'] should behave as dynamic data which changes based on users. Commented Jul 27, 2020 at 3:12
  • Why don't you try my suggestion then. Treating the template content string before sending to blade? If you want I can post an answer to describe my idea. Commented Jul 27, 2020 at 3:16

1 Answer 1

1

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 !!} 
Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help.

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.