php - CakePHP + TinyButStrong

标签 php templates cakephp tinybutstrong

有人试过将 TinyButStrong 与 CakePHP 一起使用吗? 我对 TinyButStrong 没有先验知识,但似乎是从模板生成 Word 文档的好方法。但我不确定如何将其与 CakePHP 应用程序集成。

感谢您的任何想法/建议。

最好的问候, 托尼。

最佳答案

我猜你是指 TinyButStrong 和 OpenTBS可以使用模板合并 DOCX(以及其他 Ms Office 和 OpenOffice 文档)的插件。

这是一种在 CakePHP Controller 中添加 export 操作的方法,该操作旨在生成要下载的 Docx。

以下代码适用于 CakePHP 1.3 版,未在 2.0 版中测试。

步骤:

1) 在 vendor 目录的子目录下添加 TBS 和 OpenTBS 类:

供应商/tbs/tbs_class.php
供应商/tbs/tbs_plugin_pentbs.php

2) 创建一个 CakePHP 助手来简化 TBS + OpenTBS 的准备工作:

app/views/helpers/tbs.php

<?php

class TbsHelper extends AppHelper {

    function getOpenTbs() {
        App::import('Vendor', 'tbs/tbs_class');
        App::import('Vendor', 'tbs/tbs_plugin_opentbs');

        $tbs  = new clsTinyButStrong; // new instance of TBS
        $tbs->Plugin(TBS_INSTALL, OPENTBS_PLUGIN); // load OpenTBS plugin   
        return $tbs;
    }

}

3) 现在在应该生成 Docx 的 Controller 中添加一个新的“导出”操作:

app/controllers/example_controller.php

<?php

class ExamplesController extends AppController {

    var $name = 'Examples';

    function export() {

        // Stop Cake from displaying action's execution time, this can corrupt the exported file
        // Re-ativate in order to see bugs
        Configure::write('debug',0);

        // Make the Tbs helper available in the view
        $this->helpers[] = 'Tbs';

        // Set available data in the view
        $this->set('records', $this->Example->find('all'));

    }

}

4) 最后就是创建对应的 View 。不要忘记将您的 DOCX 模板放在与 View 相同的文件夹中。

app/views/examples/export.ctp (下)
app/views/examples/export_template1.docx (使用 Ms Office 构建)

<?php

ob_end_clean(); // Just in case, to be sure

// Get a new instance of TBS with the OpenTBS plug-in
$otbs = $tbs->getOpenTbs(); 

// Load the DOCX template which is supposed to be placed in the same folder
$otbs->LoadTemplate(dirname(__FILE__).'/export_template1.docx');

// Merge data in the template
$otbs->MergeBlock('r', $records);

// End the merge and export
$file_name = 'export.docx';
$otbs->Show(OPENTBS_DOWNLOAD, $file_name);

exit; // Just in case, to be sure

TinyButStrong 提供了合并 PHP 全局变量的功能,但建议不要在 CakePHP 中使用此类功能。相反,您应该将 MergeBlock() 和 MergeField() 与 Controller 为 View 设置的数据一起使用。

如果遇到错误,请不要忘记禁用该行

Configure::write('debug', 0);

这将向您显示 CakePHP 错误。否则 CakePHP 将隐藏所有错误,包括 PHP 错误。

不要忘记 OpenTBS 也有 Debug模式。查看manual如果需要的话。

您也可以将其设为一个库(以便在您的应用程序中的任何位置使用)。

关于php - CakePHP + TinyButStrong,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8547192/

相关文章:

c++ - 如何在 C++11 中输出枚举类的值

c++ - 如何在动态数组中的某些对象上调用析构函数

c++ - 编译器优化 "constant propagation"是什么意思?

cakephp - 如何使用 $this->Html->link(..); 分配 ID 和类?

cakephp - cakephp 3:如何在表单按钮中添加类?

php - Laravel 6.x - 无法更新 Controller 方法中的表单(Nested-Child)

php - Mysql 数字在 varchar 字段中进行比较

cakephp - 如何在CakePHP 3.x中保存belongsToMany以进行编辑操作?

php - Yii2 - 尝试保存到数据库时出错

PHP:双引号不显示在输入值中,但显示在正常回显中