Twig Slimframework基础模板数据

时间:2015-08-19 07:54:46

标签: symfony twig slim

所以我一直在研究一个简单的网站,并且有一个简单的问题,我想。我有一个名为template.html的基本模板,我在每个html页面上都使用它。我的问题是,基本模板可以从数据库中获取数据吗?因为它没有正确呈现?例如,我在这里有我的404页面(我使用的是瘦框架btw)。

$app->notFound(function () use ($app) {
    $app->render('404.html');
});

里面的内容是这样的: enter image description here

如图所示,有<!-- Loaded data from the base template.html -->部分。基本模板可以从数据库中获取数据吗?如果是这样,怎么样?谢谢!

3 个答案:

答案 0 :(得分:1)

是的,基本模板可以显示数据库内容:

您必须检索所需的数据,将其渲染到要显示的特定模板,然后在基本模板中使用渲染(控制器)方法,例如

public function xyzAction(){
$data=$this->getDoctrine .... ;
return $this->render('XYZYourBundle:test.html.twig',array('data'=>$data));
}
test.html.twig中的

{℅ for d in data %}
  // do your stuff
{% endfor ℅}

以及要将其添加到基本模板的位置:

{{ render(controller('XYZYourBundle:Controller_name:xyz'))}}

你也可以将参数传递给控制器​​动作,this会有所帮助。

答案 1 :(得分:1)

您可以使用inheritance

简单方法

您的路线定义:

<?php

/**
 * Not found.
 */
$app->notFound(function () use ($app) {
    //Your amazing business to get data
    $books = array(
        "The Hobbit",
        "Leaf by Niggle",
        "The Lay of Aotrou and Itroun",
        "Farmer Giles of Ham",
        "The Homecoming of Beorhtnoth Beorhthelm's Son"
    );
    $app->render('404.html', array(
        'books' => $books
    ));
});

您的template.html:

<!DOCTYPE html>
<html>
    <head>
        {% block head %}
            <link rel="stylesheet" href="style.css" />
            <title>{% block title %}{% endblock %} - My Webpage</title>
        {% endblock %}
    </head>
    <body>
        <div id="content">
            {% block content %}
                {% block books %}
                    <h3>My magic books</h3>
                    <ul class="books">
                        {% for book in books %}
                            <li>{{ book }}</li>
                        {% endfor %}
                    </ul>
                {% endblock %}
            {% endblock %}
        </div>
    </body>
</html>

你的404.html:

{% extends "template.html" %}

{% block title %}404{% endblock %}
{% block content %}
    {% block books %}
        <div id="">
            {{ parent() }}
        </div>
    {% endblock %}
{% endblock %}

钩子方法

如果你到处都需要它,你可以使用钩子。

$app->hook('slim.before.router', function() use ($app) {
    //Your amazing business to get data
    $books = array(
        "The Hobbit",
        "Leaf by Niggle",
        "The Lay of Aotrou and Itroun",
        "Farmer Giles of Ham",
        "The Homecoming of Beorhtnoth Beorhthelm's Son"
    );
    $app->view()->setData('books', $books);
});

//And let your notFound handler be light
$app->notFound(function () use ($app) {
    $app->render('404.html');
});

答案 2 :(得分:0)

在Slim 3中,您可以使用中间件代替钩子。

BooksMiddleware.php

class BooksMiddleware{

     protected $container;

     public function __construct($container) {
         $this->container = $container;
     }

    public function __invoke($request, $response, $next){

      $sql = "SELECT * FROM books";
      $stmt = $this->container->db->prepare($sql);
      $stmt->execute();
      $books= $stmt->fetchAll(PDO::FETCH_OBJ);

      $request = $request->withAttribute('books', $books);

      $response = $next($request, $response);
      return $response;
    }
}

routes.php

$app->get('/home', '\HomeController:home')->add(BooksMiddleware::class);
$app->get('/about', '\AboutController:about')->add(BooksMiddleware::class);

HomeController.php

class HomeController{

  protected $container;

  public function __construct($container) {
      $this->container = $container;
  }

  public function home($request, $response) {

    return $this->container->view->render($response, 'home.html', [
      'books' => $request->getAttribute('books')
    ]);
  }

AboutController.php

class AboutController{

  protected $container;

  public function __construct($container) {
      $this->container = $container;
  }

  public function about($request, $response) {

    return $this->container->view->render($response, 'about.html', [
      'books' => $request->getAttribute('books')
    ]);
  }

}