【问题标题】:Building a site with Twig PHP Template Engine使用 Twig PHP 模板引擎构建网站
【发布时间】:2012-03-23 15:52:14
【问题描述】:

我已经阅读了Twig 的文档,但我不太明白如何连接这些点。

假设我创建了一个文件index.php,它实例化了Twig_Loader_FilesystemTwig_Environment 类。我可以使用loadTemplate() 在此处加载一个模板。

单个页面内容存储在.phtml.html.twig 文件中,这些文件可能链接到网站上的其他页面。但是,这些总是链接到另一个 .php 文件,而不是模板。

抽象这个过程的最佳方法是什么,以便我只需要一个 php 文件来处理多个模板?访问?某种路由器类?有没有例子?

【问题讨论】:

  • 如果它类似于模板 smarty,它是如何工作的,模板文件有 html 和 javascript,php 文件调用它们,使用模板中内置的数据链接连接数据.模板的优点是它们将 php 与 html 分开。您链接到 php 文件,因为 hte php 文件调用了 html,如果没有执行所有服务器端处理的 php 文件,html 将没有动态数据输出。
  • 我明白为什么我需要调用 PHP 文件。但是,应该如何组织服务器端来避免每个模板都需要一个 php 文件?
  • 我不确定你在问什么,我使用 smarty 的方式是,每个模板文件都有自己的 php 文件,你浏览到 php 文件,它会加载所有必要的数据,然后调用模板文件,并将数据输出给用户。使用模板,每个页面仍然需要单独的 php 文件。
  • 你是在使用某种 MVC 框架还是只是一堆 PHP 文件?
  • @JimmyBanks 我以前没有使用过 Smarty,所​​以为了比较,很高兴知道。

标签: php twig


【解决方案1】:

如果您使用多个 PHP 文件,那么创建模板渲染器类是明智的,它将引导 Twig 类、设置选项并负责查找和渲染请求的模板:

<?php
// Use correct path to Twig's autoloader file
require_once '/path/to/lib/Twig/Autoloader.php';
// Twig's autoloader will take care of loading required classes
Twig_Autoloader::register();

class TemplateRenderer
{
  public $loader; // Instance of Twig_Loader_Filesystem
  public $environment; // Instance of Twig_Environment

  public function __construct($envOptions = array(), $templateDirs = array())
  {
    // Merge default options
    // You may want to change these settings
    $envOptions += array(
      'debug' => false,
      'charset' => 'utf-8',
      'cache' => './cache', // Store cached files under cache directory
      'strict_variables' => true,
    );
    $templateDirs = array_merge(
      array('./templates'), // Base directory with all templates
      $templateDirs
    );
    $this->loader = new Twig_Loader_Filesystem($templateDirs);
    $this->environment = new Twig_Environment($this->loader, $envOptions);
  }

  public function render($templateFile, array $variables)
  {
    return $this->environment->render($templateFile, $variables);
  }
}

不要复制粘贴,这只是一个示例,您的实现可能会根据您的需要而有所不同。将此类保存在某处

用法

我将假设您的目录结构类似于:

/home/www/index.php
/home/www/products.php
/home/www/about.php

在网络服务器的根目录下创建目录(本例中为/home/www):

/home/www/templates # this will store all template files
/home/www/cache # cached templates will reside here, caching is highly recommended

把你的模板文件放到templates目录下

/home/www/templates/index.twig
/home/www/templates/products.twig
/home/www/templates/blog/categories.twig # Nested template files are allowed too

现在示例 index.php 文件:

<?php
// Include our newly created class
require_once 'TemplateRenderer.php';

// ... some code

$news = getLatestNews(); // Pulling out some data from databases, etc
$renderer = new TemplateRenderer();
// Render template passing some variables and print it
print $renderer->render('index.twig', array('news' => $news));

其他 PHP 文件类似。

注意事项

更改设置/实施以满足您的需求。您可能希望限制对templates 目录的网络访问(甚至将其放在外面的某个地方),否则每个人都可以下载模板文件。

【讨论】:

  • 这更接近我想要的。谢谢。
猜你喜欢
  • 1970-01-01
  • 2011-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-09
  • 2012-10-28
  • 1970-01-01
相关资源
最近更新 更多