master

laravel/framework

Last updated at: 29/12/2023 09:25

CompilesLayouts.php

TLDR

The CompilesLayouts.php file is a trait that is used to compile layout related statements in Blade templates. It contains methods for compiling various layout statements such as extends, sections, yields, shows, appends, overwrites, stops, and end sections.

Methods

compileExtends($expression)

Compiles the @extends statement in Blade templates into valid PHP. It generates the PHP code to include the extended template. Returns an empty string.

compileExtendsFirst($expression)

Compiles the @extendsFirst statement in Blade templates into valid PHP. It generates the PHP code to include the first matching extended template. Returns an empty string.

compileSection($expression)

Compiles the @section statement in Blade templates into valid PHP. It starts a new section in the template. Returns the generated PHP code to start the section.

compileParent()

Replaces the @parent directive in the template with a placeholder. Returns the generated PHP code to echo the placeholder.

compileYield($expression)

Compiles the @yield statement in Blade templates into valid PHP. It echoes the content of a yielded section. Returns the generated PHP code to echo the yielded content.

compileShow()

Compiles the @show statement in Blade templates into valid PHP. It echoes the content of the current section. Returns the generated PHP code to echo the section content.

compileAppend()

Compiles the @append statement in Blade templates into valid PHP. It appends content to the current section. Returns the generated PHP code to append the content to the section.

compileOverwrite()

Compiles the @overwrite statement in Blade templates into valid PHP. It stops the current section and updates its content. Returns the generated PHP code to stop the section.

compileStop()

Compiles the @stop statement in Blade templates into valid PHP. It stops the current section. Returns the generated PHP code to stop the section.

compileEndsection()

Compiles the @endsection statement in Blade templates into valid PHP. It stops the current section. Returns the generated PHP code to stop the section.

Classes

None

<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesLayouts
{
    /**
     * The name of the last section that was started.
     *
     * @var string
     */
    protected $lastSection;

    /**
     * Compile the extends statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileExtends($expression)
    {
        $expression = $this->stripParentheses($expression);

        $echo = "<?php echo \$__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";

        $this->footer[] = $echo;

        return '';
    }

    /**
     * Compile the extends-first statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileExtendsFirst($expression)
    {
        $expression = $this->stripParentheses($expression);

        $echo = "<?php echo \$__env->first({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>";

        $this->footer[] = $echo;

        return '';
    }

    /**
     * Compile the section statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileSection($expression)
    {
        $this->lastSection = trim($expression, "()'\" ");

        return "<?php \$__env->startSection{$expression}; ?>";
    }

    /**
     * Replace the @parent directive to a placeholder.
     *
     * @return string
     */
    protected function compileParent()
    {
        $escapedLastSection = strtr($this->lastSection, ['\\' => '\\\\', "'" => "\\'"]);

        return "<?php echo \Illuminate\View\Factory::parentPlaceholder('{$escapedLastSection}'); ?>";
    }

    /**
     * Compile the yield statements into valid PHP.
     *
     * @param  string  $expression
     * @return string
     */
    protected function compileYield($expression)
    {
        return "<?php echo \$__env->yieldContent{$expression}; ?>";
    }

    /**
     * Compile the show statements into valid PHP.
     *
     * @return string
     */
    protected function compileShow()
    {
        return '<?php echo $__env->yieldSection(); ?>';
    }

    /**
     * Compile the append statements into valid PHP.
     *
     * @return string
     */
    protected function compileAppend()
    {
        return '<?php $__env->appendSection(); ?>';
    }

    /**
     * Compile the overwrite statements into valid PHP.
     *
     * @return string
     */
    protected function compileOverwrite()
    {
        return '<?php $__env->stopSection(true); ?>';
    }

    /**
     * Compile the stop statements into valid PHP.
     *
     * @return string
     */
    protected function compileStop()
    {
        return '<?php $__env->stopSection(); ?>';
    }

    /**
     * Compile the end-section statements into valid PHP.
     *
     * @return string
     */
    protected function compileEndsection()
    {
        return '<?php $__env->stopSection(); ?>';
    }
}