M
M
muhasa2020-08-26 17:44:54
Laravel
muhasa, 2020-08-26 17:44:54

Blade outside of Laravel. How to render a template to a variable without a response object?

It so happened that I'm building a project on a slim framework , but a blade is screwed there . Of course there are no helpers, facades and other joys. I came across a task - you need to take a letter template, substitute the necessary variables and pass it to the mail sending object as a string. Of course, the function $blade->render()requires the first parameter $response .

Technically, I solved the problem, but it was crooked - I just passed it new Response() , the template returned the Response object to me, and through the methods ->getBody()->__toString()
everything worked out as it should.

But such dances with a tambourine come from one space - I don't know how to solve all this with native Blade tools. Obviously there is some method that allows you to render a template into a text variable? I searched on the Internet, but there all the methods go through the facades and helpers of Laravel, it does not suit me.

Thank you for your help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daria Motorina, 2020-08-26
@glaphire

Perhaps this package is suitable, there is just a blade without Laravel
https://github.com/jenssegers/blade

A
artoodetoo, 2020-08-27
@artoodetoo

https://gist.github.com/AlexR1712/83e502e25300fcfe... Bottom
line:
You can compile a string as a template in Blade::compileString() , then execute the resulting code, substituting variable values ​​through extract(), for example. Everything that this code outputs must be caught through the ob_* buffered output functions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question