Is that possible to use laravel blade outside the view folder?

北城以北 提交于 2020-08-21 19:42:27

问题


I got a wordpress blog inside the public sub folder.

I wanted to use same layout with the laravel view that using blade.

Is there anyway to achieve that?


回答1:


I managed to do this with the following function:

function bladeCompile ($from, $to, $data)
{
    $fs = new \Illuminate\Filesystem\Filesystem;
    $b = new \Illuminate\View\Compilers\BladeCompiler($fs, __DIR__);
    $src = $b->compileString (file_get_contents($from));

    $isPhp = false;
    if (substr( $src, 0, 5 ) === "<?php")
    {
        $isPhp = true;
        $src = substr($src, 5);
    }
    $tempFileName = tempnam("/tmp", "blade-compile");
    file_put_contents($tempFileName, $src);

    ob_start();

    extract($data);

    include $tempFileName;
    $out = ob_get_clean();
    if ($isPhp)
    {
        $out = '<?php'.$out;
    }
    file_put_contents($to, $out);
}

And then use with:

$data = array ( // equivalent to the 'with' function.
    'parameter' => 'value';
    );
bladeCompile ('input.blade.file', 'result.file', $data);



回答2:


Yous just need to add your paths to app/config/view.php and blade will automatically find them




回答3:


You can define a custom namespace for easier usage:

// Register your custom namespace in your AppServiceProvider in the boot() method
view()->addNamespace('custom_views', app_path('custom_path'));

// usage:
view('custom_views::some.view.name')


来源:https://stackoverflow.com/questions/17079369/is-that-possible-to-use-laravel-blade-outside-the-view-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!