Twig - 基础使用

2019-06-25  本文已影响0人  三笠丶阿尔曼

一篇简短的介绍

<?php

require_once "../vendor/autoload.php";

use Twig\Environment; 


$loader = new \Twig\Loader\ArrayLoader([
    'index' => 'Hello {{ name }}!',
]);

$twig = new \Twig\Environment($loader);

$twig = new Environment($loader, array(
    'cache' => 'cache',
    'auto_reload' => true,  //根据文件更新时间,自动更新缓存
    'debug' => true
));

echo $twig->render('index', ['name' => 'Fabien']);


$loader = new \Twig\Loader\FilesystemLoader('templates');
$twig = new \Twig\Environment($loader, [
    'cache' => 'cache',
    'auto_reload' => true,  //根据文件更新时间,自动更新缓存
    'debug' => true
]);

echo $twig->render('hello', ['name' => 'Sanli']);



echo "<br>——————————————— Chain ——————————————— <br>";
$loader1 = new \Twig\Loader\ArrayLoader([
    'base.html' => '{% block content %}{% endblock %}',
]);
$loader2 = new \Twig\Loader\ArrayLoader([
    'index.html' => '{% extends "base.html" %}{% block content %}Hello {{ name }}{% endblock %}',
    'base.html'  => 'Will never be loaded',
]);

$loader = new \Twig\Loader\ChainLoader([$loader1, $loader2]);


$twig = new \Twig\Environment($loader, [
    'cache' => 'cache',
    'auto_reload' => true,  //根据文件更新时间,自动更新缓存
    'debug' => true
]);

echo $twig->render('index.html', ['name' => 'Sanli']);


上一篇 下一篇

猜你喜欢

热点阅读