Render Blade from string instead of File
Hope this helps,
https://github.com/TerrePorter/StringBladeCompiler
This is a fork of the next link that removes the db model requirement and replaces it with a array that requires three required keys ('template', 'cache_key', 'updated_at') instead of the full Eloquent model.
https://github.com/Flynsarmy/laravel-db-blade-compiler
This uses a Eloquent model to get the template.
Compile Laravel blade component from string
After some digging, this was actually very easy. Laravel always delivers
$replacers = [
'x-system-status' => view('compontents.system-status', $data)->toHtml(),
];
$page = \App\Page::first();
$text = $page->content;
foreach ($replacers as $key => $value) {
$text = str_replace('{'.$key.'}', $value, $text);
}
Laravel: how to create a rendered view from a string instead of a blade file?
You can use Blade Facade.
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Blade;
public function __invoke()
{
$name='Peter Pan';
return Blade::render("
<h1> Hello {$name} </h1>
",['name'=>$name]);
}
Laravel 5 - Compile String and Interpolate Using Blade API on Server
This should do it:
// CustomBladeCompiler.php
use Symfony\Component\Debug\Exception\FatalThrowableError;
class CustomBladeCompiler
{
public static function render($string, $data)
{
$php = Blade::compileString($string);
$obLevel = ob_get_level();
ob_start();
extract($data, EXTR_SKIP);
try {
eval('?' . '>' . $php);
} catch (Exception $e) {
while (ob_get_level() > $obLevel) ob_end_clean();
throw $e;
} catch (Throwable $e) {
while (ob_get_level() > $obLevel) ob_end_clean();
throw new FatalThrowableError($e);
}
return ob_get_clean();
}
}
Usage:
$first_name = 'Joe';
$dbString = '<p>Welcome {{ $first_name }},</p>';
return CustomBladeCompiler::render($dbString, ['first_name' => $first_name]);
Thanks to @tobia on the Laracasts forums.
Render Laravel blade template as string without escaped chars and line breaks?
If you want it to be html, use method toHtml()
view('holiday.diagram')->toHtml();
Related Topics
How to Successfully Rewrite Old MySQL-PHP Code With Deprecated MySQL_* Functions
Including PHP File from Another Server With PHP
How to Clear Browser Cache With PHP
PHP Password_Hash(), Password_Verify()
Why Is Register_Globals So Bad
How to Handle Ipv6 Addresses in PHP
Localhost Vs. 127.0.0.1 in MySQL_Connect()
How to Embed a .Png Image into an HTML Page
Best Practice Multi Language Website
How to Write to the Console in PHP
Get Absolute Path of Initially Run Script
What Is the Function _Construct Used For
Get First 100 Characters from String, Respecting Full Words
Twitter API Returns Error 215, Bad Authentication Data
Laravel Certificate Verification Errors When Sending Tls Email
How to Check If a Word Is Contained in Another String Using PHP