美文网首页
Twig - 基础使用

Twig - 基础使用

作者: 三笠丶阿尔曼 | 来源:发表于2019-06-25 10:29 被阅读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']);
    
    
    

    相关文章

      网友评论

          本文标题:Twig - 基础使用

          本文链接:https://www.haomeiwen.com/subject/wpkvqctx.html