【发布时间】:2015-06-26 11:24:35
【问题描述】:
我正在尝试创建一个使用 Twigbridge 而不是 .php 视图的 Laravel 5.1 包。我可以获得正常的.php 视图以正常显示,但是当我尝试使用.twig 模板时,我只会遇到错误。在为 Laravel 包创建视图时是否可以使用 Twigbridge 模板,或者我们几乎坚持使用 blade 和 php?
编辑 1: 在不扩展任何内容的情况下加载 Twig 模板。当我开始使用{% extends 'file' %} 时,错误就会出现。
我收到此错误
Error loading /virtual/laravel/packages/username/mypackage/src/Views/sample.twig:
Template "templates.master" is not defined () in
"/virtual/laravel/packages/username/mypackage/src/Views/sample.twig" at line 1.
我的文件夹
username
|_ mypackage
|_ src
|_ Controllers
- SampleController.php
|_ Views
|_ templates
- master.twig
- sample.twig
树枝模板sample.twig
{% extends 'templates.master' %}
{% block content %}
{{ parent() }}
{% endblock %}
我的 templates/master.twig 文件中有 {% block content %} Hello World {% endblock %}。
我的composer.json
{
"name": "username/mypackage",
"description": "",
"license": "MIT",
"keywords": ["laravel"],
"authors": [
{
"name": "username",
"email": "username@gmail.com"
}
],
"require": {
"rcrowe/twigbridge": "0.7.x"
},
"minimum-stability": "stable"
}
我的服务提供商(简体)
<?php namespace Username\MyPackage;
use Illuminate\Support\ServiceProvider;
class MyPackageServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
// Route
include __DIR__.'/routes.php';
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
// This line works fine for normal .php views but for some reason not for .twig views
$this->loadViewsFrom(__DIR__ . '/Views', 'mypackage');
// Providers
$this->app->register('TwigBridge\ServiceProvider');
// Aliases
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
$loader->alias('Twig', 'TwigBridge\Facade\Twig');
$this->app['mypackage'] = $this->app->share(function($app) {
return new MyPackage;
});
}
/**
* Register a view file namespace.
*
* @param string $namespace
* @param string $path
* @return void
*/
protected function loadViewsFrom($path, $namespace)
{
if (is_dir($appPath = $this->app->basePath().'/resources/views/vendor/'.$namespace)) {
$this->app['view']->addNamespace($namespace, $appPath);
}
$this->app['view']->addNamespace($namespace, $path);
}
/*
*/
}
控制器非常基础
<?php namespace Username\MyPackage\Controllers;
use App\Http\Controllers\Controller;
class SampleController extends Controller {
public function sample() {
return view('mypackage::sample');
}
}
【问题讨论】: