■ 백엔드 ■/Slim2018.05.24 11:54

Twig views


composer require slim/twig-view


Configure the view

<?php
return[
    // ...
    'view' => [
        'template_path' => 'app/templates',
        'twig' =>[
            'cache' => 'cache/twig',
            'debug' => true,
            'auto_reload' => true,
        ],
    ],
];



Register the view

<?php
// Create the view object
$view = new \Slim\Views\Twig(
    $settings['view']['template_path'],
    $settings['twig']);

// add extensions
$twig = $view->getEnvironment();
$twig->addExtension(new Twig_Extension_Debug());

$app->register($app['TwigView']);



Template

<html>
  <head>
    <title>Hello {{ name }}</title>
    <link rel='stylesheet' href='/css/style.css'>
  </head>
  <body>
     <h1>Hello {{ name }}</h1>
  </body>
</html>


Render

<?php
$app->get('/hello/{name}', function($request, $response, $args) (use $app){
   $body = $app['view']->fetch('hello.twig', [
      'name' => $args['name'],
   ]);
 
   return $response->write($body);
});



결과





'■ 백엔드 ■ > Slim' 카테고리의 다른 글

Slim Framework 3 Skeleton Application  (0) 2018.06.25
Twig views  (0) 2018.05.24
Middleware  (0) 2018.05.24
Routes  (0) 2018.05.24
index.php  (0) 2018.05.24
03. 웹서버 설정  (0) 2018.05.23
02. 컴포저로 slim 설치하기  (0) 2018.05.22
01. slim 프레임워크  (0) 2018.05.22
Posted by SAP (Study And Programming) by serpiko

댓글을 달아 주세요