简体   繁体   English

用TWIG加载宏

[英]Load macro with 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 :( 我在项目中安装了TWIG,但无法加载宏,我想创建一个文件测试(例如testmacro.twig),该文件调用我的宏(例如test.twig),但我不能:(

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 : 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 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 ! 提前致谢 !

Template paths are relative to the folder you've set as parameter in the Twig_Loader_Filesystem : 模板路径相对于您在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 , ... : 因此,在您的情况下,模板路径从文件夹templates开始,这对于includesmacros ...也是正确的:

{% import 'test.twig' as test %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM