duanluangua8850 2016-07-25 21:24
浏览 98
已采纳

用TWIG加载宏

I installed TWIG in my project but I can't load my macro, I would like to create a file tests (For example testmacro.twig) who call my macro (For example test.twig), but I can not :(

test.twig

{% macro test (options) %}

    {% if options.titre is not empty %}
        coucou {{ options.titre }}
    {% endif %}     

{% endmacro }

testmacro.twig

{% import "/home/valentin/Bureau/htdocs/templates/test.twig" as test %}

{{ test.test ({ titre : 'aaaaaaah' }) }}

Error :

Fatal error: Uncaught exception 'Twig_Error_Loader' with message 'Unable to find template "/home/valentin/Bureau/htdocs/templates/test.twig" (looked into: templates) in "testmacro.twig" at line 1.' in /opt/lampp/htdocs/Twig/Loader/Filesystem.php:215 Stack trace: #0 /opt/lampp/htdocs/Twig/Loader/Filesystem.php(139): Twig_Loader_Filesystem->findTemplate('/home/valentin/...') #1 /opt/lampp/htdocs/Twig/Environment.php(312): Twig_Loader_Filesystem->getCacheKey('/home/valentin/...') #2 /opt/lampp/htdocs/Twig/Environment.php(378): Twig_Environment->getTemplateClass('/home/valentin/...', NULL) #3 /opt/lampp/htdocs/Twig/Template.php(286): Twig_Environment->loadTemplate('/home/valentin/...', NULL) #4 /opt/lampp/htdocs/Twig/Environment.php(403) : eval()'d code(19): Twig_Template->loadTemplate('/home/valentin/...', 'testmacro.twig', 1) #5 /opt/lampp/htdocs/Twig/Template.php(387): __TwigTemplate_e0a6b64206d91d57c5131f111d33ce0450565dc8429fccbaf4af635b1ac6ec27->doDisplay(Array, Array) #6 /opt/lampp/htdocs/Twig/Template.php(355): Twig_T in /opt/lampp/htdocs/Twig/Loader/Filesystem.php on line 215

twig.php :

<?php
include_once('Twig/Autoloader.php');
Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem('templates'); // Dossier contenant les templates
$twig = new Twig_Environment($loader, array(
  'cache' => false
));

index.php

<?php
include('twig.php');
$template = $twig->loadTemplate('testmacro.twig');
echo $template->render(array()); 

?>

I don't understand .. :/

Can you help me please ? Thanks in advance !

  • 写回答

1条回答 默认 最新

  • dtf54486 2016-07-26 05:40
    关注

    Template paths are relative to the folder you've set as parameter in the Twig_Loader_Filesystem:

    $loader = new Twig_Loader_Filesystem('templates');
    

    So in your case template paths start from the folder templates, this is also correct for includes, macros, ... :

    {% import 'test.twig' as test %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?
  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?