【问题标题】:Export rendered html files as zip from modx从 modx 将渲染的 html 文件导出为 zip
【发布时间】:2020-04-15 20:25:29
【问题描述】:

首先我的目标是为 modx 编写一个 Extras 以将网站内容导出为 epub 文件。 我已经搜索过是否存在类似的东西,但我没有找到任何东西。有没有人知道这样的附加功能?或者任何人都可以建议我在 modx 中做到这一点的最佳方法吗?

我的想法是收集所有呈现的 html 文件和资源,然后使用基于 php 的 epub 库生成 epub 文件。 但是我没有找到从 modx 获取渲染的 html 文件的方法。 我可以获取模板,也可以获取 html sn-p 代码,但我需要整个 html 文件。

MODX 信息:

  • MODX 版本:MODX 革命 2.7.3-pl(传统)

  • 版本-代号:Revolution

【问题讨论】:

    标签: php content-management-system modx modx-revolution modx-resources


    【解决方案1】:

    有多种方法可以解决这个问题。

    您可以编写一个独立的 PHP 应用程序,该应用程序从 XML 站点地图或链接数组调用您网站上的所有 URL,然后使用 file_get_contents() 函数下载呈现的 HTML(请参阅文档 here)。 类似这样的东西(以下代码未经测试):

    // [filename] => [URL]
    $pages = array(
       'index.html' => 'https://example.com/',
       'contact.html' => 'https://example.com/contact.html',
    );
    
    foreach($pages as $filename => $link){
       $filePath = $_SERVER['DOCUMENT_ROOT'].'/'.$filename;
       $html= file_get_contents($link);
       $handle = fopen($filePath,"w");
       fwrite($handle,$html);
       fclose($handle);
    }
    

    或者,您可以在 MODX 中编写 snippet 并直接使用 xPDO 获取资源 URL。以下代码将获取所有资源的链接和文件名,并排除进程中的网络链接、符号链接和静态资源。如果你打算在你的包中实现以下代码,你需要稍微调整一下。

    $resources = $modx->getIterator('modResource', array(
        'class_key' => 'modDocument',
    ));
    $pages = array();
    foreach($resources as $resource){
        $pages[$resource->get('alias').'.html'] = $modx->makeUrl($resource->get('id'), '', '', 'full');
    }
    

    上述代码中$pages数组的输出如下:

    Array
    (
        [index.html] => https://example.com/
        [test.html] => https://example.com/test.html
    )
    

    【讨论】:

      猜你喜欢
      • 2013-08-15
      • 2018-01-17
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 2021-01-05
      相关资源
      最近更新 更多