【问题标题】:How do I install the Twig Template Engine?如何安装 Twig 模板引擎?
【发布时间】:2010-08-08 23:46:31
【问题描述】:

我正在运行 Mamp 作为我的本地服务器。我已经在/Applications/MAMP/svn/twig/twig/lib 中安装了 Twig。我已经在我的 php.ini 文件中包含了这个路径:

include_path = ".:/Applications/MAMP/bin/php5.3/lib/php:/Applications/MAMP/svn/zendframework/trunk/library:/Applications/MAMP/svn/twig/twig/lib";

为了完成安装和访问 Twig,需要进入我的 htdocs 文件夹中的哪些内容?

【问题讨论】:

  • 请问您在使用 Zend 框架时为什么还要使用 twig?我建议您一般远离 PHP 模板引擎。 Here's why.
  • 我只是在做一个需要模板引擎的教程。不过感谢您提供的信息......

标签: php twig mamp template-engine


【解决方案1】:

你不需要安装任何东西,你可以在 PHP 中使用它。这是一个加载和渲染模板的简单脚本:

require_once( "Twig/Autoloader.php" );

Twig_Autoloader::register();
// Load template files from the ./tpl/ folder and use ./tpl/cache/ for caching
$twig = new Twig_Environment( new Twig_Loader_Filesystem("./tpl"),
    array( "cache" => "./tpl/cache" ) );

// Load and render 'template.tpl'
$tpl = $twig->loadTemplate( "template.tpl" );
echo $tpl->render( array("msg"=>"Hello, World!") );

您的 template.tpl 可能如下所示:

<html>
    <!-- ... -->
    <body>
        <h1>{{ msg|e }}</h1>
    </body>
</html>

此示例将转义并回显“Hello, World”。

有关更多信息,请阅读 (PHP) developperstemplate designers 的文档。

【讨论】:

    【解决方案2】:
    include __DIR__ . "/vendor/twig/twig/lib/Twig/Autoloader.php";  
    
    //register autoloader  
    
    Twig_Autoloader::register();  
    
    //loader for template files  
    
    $loader = new Twig_Loader_Filesystem('templates');  
    
    //twig instance  
    
    $twig = new Twig_Environment($loader, array('cache' => 'cache'));  
    
    //load template file  
    
    $template = $twig->loadTemplate('index.html');  
    
    //render a template  
    
    echo $template->render(array('title' => 'Welcome to Twig template'));  
    

    找到更多关于这个Tutorial的信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 2012-10-28
      • 2014-07-20
      • 2011-10-27
      • 1970-01-01
      • 2012-12-21
      相关资源
      最近更新 更多