Wie können Blade-Vorlagen aus Strings in Laravel kompiliert werden?

Patricia Arquette
Freigeben: 2024-10-22 12:03:02
Original
123 Leute haben es durchsucht

How Can Blade Templates Be Compiled from Strings in Laravel?

Compiling Blade Templates from Strings

In Laravel, a popular web application framework, Blade is the default template engine. Typically, Blade views are stored in view files. However, a question arose regarding the possibility of compiling Blade templates from strings rather than view files.

The provided code demonstrates an attempt to compile a Blade template from a string:

<code class="php">$string = '<h2>{{ $name }}</h2>';
echo Blade::compile($string, array('name' => 'John Doe'));</code>
Nach dem Login kopieren

To extend the functionality of BladeCompiler, a custom BladeCompiler class was created. The new compileWiths() method takes a string and an array of arguments as inputs. It compiles the string, extracts the passed arguments, and evaluates the compiled code within an exception handler.

The updated code below shows the implementation:

<code class="php">namespace Laravel\Enhanced;

use Illuminate\View\Compilers\BladeCompiler as LaravelBladeCompiler;

class BladeCompiler extends LaravelBladeCompiler {

    /**
     * Compile blade template with passing arguments.
     *
     * @param string $value HTML-code including blade
     * @param array $args Array of values used in blade
     * @return string
     */
    public function compileWiths($value, array $args = array())
    {
        $generated = parent::compileString($value);

        ob_start() and extract($args, EXTR_SKIP);

        // We'll include the view contents for parsing within a catcher
        // so we can avoid any WSOD errors. If an exception occurs we
        // will throw it out to the exception handler.
        try
        {
            eval('?&gt;'.$generated);
        }

        // If we caught an exception, we'll silently flush the output
        // buffer so that no partially rendered views get thrown out
        // to the client and confuse the user with junk.
        catch (\Exception $e)
        {
            ob_get_clean(); throw $e;
        }

        $content = ob_get_clean();

        return $content;
    }

}</code>
Nach dem Login kopieren

Das obige ist der detaillierte Inhalt vonWie können Blade-Vorlagen aus Strings in Laravel kompiliert werden?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!