首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我能用拉拉维尔外面的叶片模板引擎吗?

我能用拉拉维尔外面的叶片模板引擎吗?
EN

Stack Overflow用户
提问于 2016-10-26 23:32:01
回答 4查看 20.6K关注 0票数 20

我想创建一个设计模式,并使用“叶片模板引擎”。我能在Laravel外面使用叶片模板引擎并在我的新模式中使用它吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-05-22 20:43:48

请记录在案:

我测试了许多库以在Laravel之外运行刀片(我不使用),而且大多数库都是原始库的拙劣的黑客,它们只是简单地复制和粘贴代码并删除了一些依赖项,但它保留了Laravel的许多依赖项。

因此,我(为一个项目)在单个文件中为刀片创建了一个免费的替代方案(麻省理工学院许可,即关闭源代码/私有代码是OK的),并且不依赖于外部库。您可以下载该类并开始使用它,也可以通过composer安装。

https://github.com/EFTEC/BladeOne

https://packagist.org/packages/eftec/bladeone

没有Laravel自己的特性(扩展),它是100%兼容的。

它的工作原理:

代码语言:javascript
复制
<?php
include "lib/BladeOne/BladeOne.php";
use eftec\bladeone;

$views = __DIR__ . '/views'; // folder where is located the templates
$compiledFolder = __DIR__ . '/compiled';
$blade=new bladeone\BladeOne($views,$compiledFolder);
echo $blade->run("Test.hello", ["name" => "hola mundo"]);
?>

另一种选择是使用树枝,但我测试了它,我不喜欢它。我喜欢Laravel的语法,它接近ASP.NET MVC Razor。

编辑:到目前为止(2018年7月),它实际上是唯一一个没有Laravel支持Blade 5.6新特性的模板系统。;-)

票数 33
EN

Stack Overflow用户

发布于 2016-11-13 10:20:15

你当然可以,有很多独立的叶片选择的包装,只要你是舒适的作曲家,那么应该没有问题,这一个看上去相当有趣,因为有一个非常高的明星百分比相比,下载。

请注意,虽然我没有亲自尝试过,就像你一样,我正在为我自己的项目寻找一个独立的选择,并偶然发现,我会给它一个真正的好锻炼,尽管在不久的将来的某个时候,

票数 1
EN

Stack Overflow用户

发布于 2020-01-13 15:00:29

Matt创建了一个完整的存储库,向您展示了如何在Laravel之外直接使用各种照明组件。我建议学习他的例子,看看他的源代码。

https://github.com/mattstauffer/Torch

这里是在Laravel之外使用Laravel视图的index.php

https://github.com/mattstauffer/Torch/blob/master/components/view/index.php

您可以在它周围编写一个自定义包装器,这样您就可以像Laravel一样调用它。

代码语言:javascript
复制
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\View\Factory;
use Illuminate\View\FileViewFinder;

function view($viewName, $templateData)
{
    // Configuration
    // Note that you can set several directories where your templates are located
    $pathsToTemplates = [__DIR__ . '/templates'];
    $pathToCompiledTemplates = __DIR__ . '/compiled';

    // Dependencies
    $filesystem = new Filesystem;
    $eventDispatcher = new Dispatcher(new Container);

    // Create View Factory capable of rendering PHP and Blade templates
    $viewResolver = new EngineResolver;
    $bladeCompiler = new BladeCompiler($filesystem, $pathToCompiledTemplates);

    $viewResolver->register('blade', function () use ($bladeCompiler) {
        return new CompilerEngine($bladeCompiler);
    });

    $viewResolver->register('php', function () {
        return new PhpEngine;
    });

    $viewFinder = new FileViewFinder($filesystem, $pathsToTemplates);
    $viewFactory = new Factory($viewResolver, $viewFinder, $eventDispatcher);

    // Render template
    return $viewFactory->make($viewName, $templateData)->render();
}

然后,您可以使用以下方法调用它

代码语言:javascript
复制
view('view.name', ['title' => 'Title', 'text' => 'This is text']);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40273762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档