geo*_*get 3 php templates cakephp tinybutstrong
有没有人尝试过使用TinyButStrong和CakePHP?我没有TinyButStrong的先验知识,但似乎是从模板生成Word文档的好方法.但我不确定如何将其与CakePHP应用程序集成.
感谢您提出任何意见/建议.
最好的问候,托尼.
我认为你的意思是TinyButStrong与OpenTBS插件,它可以使用模板合并DOCX(和其他Ms Office和OpenOffice文档).
这是一种在CakePHP控制器中添加导出操作的方法,该控制器旨在生成要下载的Docx.
以下代码适用于CakePHP 1.3版,未经2.0版测试.
脚步 :
1)在供应商目录中的子目录下添加TBS和OpenTBS类:
vendors/tbs/tbs_class.php
供应商/ tbs/tbs_plugin_opentbs.php
2)创建一个CakePHP助手,简化TBS + OpenTBS的准备工作:
应用程序/视图/助理/ 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;
}
}
Run Code Online (Sandbox Code Playgroud)
3)现在在应该生成Docx的控制器中添加一个新的"导出"操作:
应用程序/控制器/ 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'));
}
}
Run Code Online (Sandbox Code Playgroud)
4)最后一件事是创建相应的视图.不要忘记将DOCX模板放在与视图相同的文件夹中.
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
Run Code Online (Sandbox Code Playgroud)
TinyButStrong提供了合并PHP全局变量的功能,但建议不要在CakePHP中使用此类功能.相反,您应该使用MergeBlock()和MergeField()以及Controller为View设置的数据.
如果遇到错误,请不要忘记禁用该行
Configure::write('debug', 0);
Run Code Online (Sandbox Code Playgroud)
这将显示CakePHP错误.否则CakePHP将隐藏所有错误,包括PHP错误.
不要忘记OpenTBS也有调试模式.如果需要,请参阅手册.
您也可以将其设为lib(在应用程序的任何位置使用).
归档时间: |
|
查看次数: |
1172 次 |
最近记录: |