我在模板中阅读了管理资产的Silex Cookbook
http://silex.sensiolabs.org/doc/cookbook/assets.html
并在我的app/app.php
:
$app['twig'] = $app->share($app->extend('twig', function ($twig, $app) {
$twig->addFunction(new \Twig_SimpleFunction('asset', function ($asset) {
// implement whatever logic you need to determine the asset path
return sprintf('http://assets.examples.com/%s', ltrim($asset, '/'));
}));
return $twig;
}));
$app->get('/', function () use ($app) {
return $app['twig']->render('index.twig', array(
'title' => "Hello World",
'colors' => array("red", "green", "yellow"),
));
});
和index.twig
包含:
{% extends "layout.twig" %}
{% block title %}
{{ title }}
{% endblock %}
{% block content %}
<h1>{{ title }}</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Animi quibusdam numquam laudantium eum asperiores non libero odio quae debitis beatae perferendis eius esse molestiae voluptatum vel inventore quasi. Quo sint sequi sunt amet sapiente tempora autem iusto praesentium rerum ducimus.</p>
<ul>
{% for color in colors %}
<li>{{ color }}</li>
{% endfor %}
</ul>
{{ asset('/css/styles.css') }}
{% endblock %}
一切正常,但当我在另一个asset
文件中使用twig
时,我会收到此错误:
Twig_Error_Syntax in Parser.php line 370:
A template that extends another one cannot have a body in "admin/dashboard.twig" at line 3.
例如,我的AdminDashboard
控制器包含:
<?php
namespace App\Controller\Admin;
use Silex\Application;
class AdminDashboard
{
function __construct()
{
return "Dashboard";
}
function indexAction(Application $app)
{
return $app['twig']->render('admin/dashboard.twig', array(
'title' => "Hello World",
'colors' => array("red", "green", "yellow"),
));
}
}
和admin/dashboard.twig
包含:
{% extends "layout.twig" %}
{{ asset('/css/styles.css') }}
但是当我访问我的admin
页面时,我遇到了错误。
答案 0 :(得分:0)
只需阅读错误消息,它与函数调用无关:
扩展另一个模板的模板不能有正文
您的admin/dashboard.twig
文件在第一行有{% extends ... %}
,这意味着它会扩展另一个模板。正如您在错误消息中看到的那样,扩展另一个模板的模板不能具有正文。它只能填写父模板定义的块。