脚本宝典收集整理的这篇文章主要介绍了CakePHP TinyButStrong,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
感谢您提出任何意见/建议.
最好的祝福,
托尼.
这是一种在CakePHP控制器中添加导出操作的方法,该控制器旨在生成要下载的Docx.
脚步 :
1)在供应商目录中的子目录下添加TBS和OpenTBS类:
供应商/ 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; } }
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')); } }
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
TinyButStrong提供了合并PHP全局变量的功能,但建议不要在CakePHP中使用此类功能.相反,您应该使用MergeBlock()和MergeField()以及Controller为View设置的数据.
如果遇到错误,请不要忘记禁用该行
Configure::write('debug',0);
这将显示CakePHP错误.否则CakePHP将隐藏所有错误,包括PHP错误.
不要忘记OpenTBS也有调试模式.如果需要,请参见manual.
您也可以将其设为lib(在应用程序的任何位置使用).
以上是脚本宝典为你收集整理的CakePHP TinyButStrong全部内容,希望文章能够帮你解决CakePHP TinyButStrong所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。