Drupal可以根据url中的文件扩展名更改使用的模板吗?

时间:2010-06-18 21:25:39

标签: drupal file-extension

我正在使用Drupal 6.16并安装了许多模块。我试图找出当将不同的文件扩展名添加到url时是否有办法更改节点的输出。例如:

http://example.com/drupal?q=foo/bar - returns a normal drupal node
http://example.com/drupal?q=foo/bar.xml - returns xml output of the node

Drupal甚至可以实现这一点吗?我是否必须破解核心代码才能使其正常工作?

2 个答案:

答案 0 :(得分:1)

您不需要hack the core代码。可能有几个贡献的模块可以为你做到这一点。

要输出节点的XML版本,请查看Views Bonus Pack模块,该模块扩展了视图模块。它具有基本的导出功能,包括CSV,TXT,DOC和XML。文档很简短,但views_bonus / export /目录中有一个README.txt文件,它提供了在输出XML的视图中创建feed的基本步骤。

您可以设置Feed的路径,因此虽然我认为.xml扩展名不起作用,但您可以设置一个包含其他组件的路径:

http://example.com/drupal?q=foo/bar      <-- normal output
http://example.com/drupal?q=foo/bar/xml  <-- XML output

要根据路径更改用于节点的模板文件,可以使用template.php文件中的preprocess function根据路径添加template suggestion。这需要更多地了解模板文件的工作方式,但最终您将比使用视图更能控制输出。

答案 1 :(得分:1)

以下是我修复此问题的方法。

  1. 添加custom_url_rewrite_inbound函数以检查以.xml结尾的传入请求。如果它找到以.xml结尾的请求,则将其删除,以便其余的drupal机器可以找到正确的数据。它还将“subsite_xml_request”设置为true,以便稍后可以使用相应的主题模板。

    function custom_url_rewrite_inbound (&$result, $path, $path_language) {
      if(preg_match('/\.xml$/', $path)) {
        $search = preg_replace('/^(.*)\.xml$/', "$1", $path);
        if ($src = drupal_lookup_path('source', $search, $path_language)) {
          $_REQUEST['xml_request'] = true;
          $result = $src;
        }
    }
    
  2. 修改template.php中的phptemplate_preprocess_page功能,添加其他'-xml'模板。

    function phptemplate_preprocess_page(&$vars) {   
      if ($_REQUEST['xml_request']) {
        if (module_exists('path')) {
          $path = str_replace('/edit','',$_GET['q']);
          $alias = drupal_get_path_alias($path);
          if ($alias != $_GET['q']) {
            $template_filename = 'page';
            foreach (explode('/', $alias) as $path_part) {
              $template_filename = $template_filename . '-' . $path_part;
              $vars['template_files'][] = $template_filename . '-xml';
            }
            $vars['template_files'][] = 'page-xml';
          }
        }
      }
    }
    
  3. 创建所需的page-xml.tpl.php