CakePHP + TinyButStrong [英] CakePHP + TinyButStrong

查看:354
本文介绍了CakePHP + TinyButStrong的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Anyone tried using TinyButStrong together with CakePHP? I have no prior knowledge of TinyButStrong but seems to be a good way to generate Word documents from templates. But I am not sure how to integrate this with a CakePHP application.

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

Thank you for any ideas / suggestions.

推荐答案

我假设你的意思是TinyButStrong with the OpenTBS 插件,它可以使用模板来合并DOCX(以及其他Office和OpenOffice文档)。

I presume you mean TinyButStrong with the OpenTBS plug-in which can merge DOCX (and other Ms Office and OpenOffice documents) using templates.

以下是在CakePHP控制器中添加导出操作的方法,该控制器用于生成要下载的Docx。

Here is a way to add an export action in a CakePHP Controller which is destined to generate a Docx to be downloaded.

以下代码适用于CakePHP版本1.3,它未使用版本2.0测试。

The following code is available for CakePHP version 1.3, it is not tested with version 2.0.

步骤:

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

1) Add the TBS and OpenTBS classes in the vendor directory, under a subdirectory:

vendors / tbs / tbs_class.php

vendors / tbs / tbs_plugin_opentbs.php

2)创建一个CakePHP帮助程序,简化TBS + OpenTBS的准备工作:

2) Create a CakePHP helper that will simplify the preparation of 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:

3) Now add a new "export" action in the controller that should generate the Docx:

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'));

    }

}

最后一件事是创建相应的视图。不要忘记将您的DOCX模板放置在与视图相同的文件夹中。

4) The last thing is to create the corresponding view. Don't forget to place your DOCX template in the same folder as the view.

app / views / examples / export.ctp (以下)

app / views / examples / export_template1.docx b
$ b

app/views/examples/export.ctp (below)
app/views/examples/export_template1.docx (to build with 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为Controller设置的数据。

TinyButStrong gives facilities to merge PHP global variables, but it is recommended to not use such feature within CakePHP. Instead, you should use MergeBlock() and MergeField() with the data set by the Controller for the View.

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

If you met bugs, don't forget to disable the line

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

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

and this will show you the CakePHP errors. Otherwise CakePHP will hide all errors including PHP errors.

不要忘记OpenTBS也有一个调试模式。如有需要,请参阅手册

Don't forget that OpenTBS has also a debug mode. See the manual if needed.

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

You can also make this a lib (to be used anywhere in your application).

这篇关于CakePHP + TinyButStrong的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆