CakePHP + TinyButStrong - php

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.
Best regards,
Tony.

I presume you mean TinyButStrong with the OpenTBS plug-in which can merge DOCX (and other Ms Office and OpenOffice documents) using templates.
Here is a way to add an export action in a CakePHP Controller which is destined to generate a Docx to be downloaded.
The following code is available for CakePHP version 1.3, it is not tested with version 2.0.
Steps :
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) 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) 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'));
}
}
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 (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 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);
and this will show you the CakePHP errors. Otherwise CakePHP will hide all errors including PHP errors.
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).

Related

Yii2: How to force using fallback MessageFormatter method?

My website is with a hosting provider that has the MessageFormatter class available on the server (Linux, PHP 7.0.27) but it is an old ICU version (4.2.1) that doesn't support my message {number,plural,=0{# available} =1{# available} other{# available}} and gives the error:
Message pattern is invalid: Constructor failed
msgfmt_create: message formatter creation failed: U_ILLEGAL_CHARACTER
...because of the =1 and =2 notation.
I'm not able to make changes to the server so how can I force using the fallback method provided by Yii2 which works just fine?
There is this hacky way you can try.
Copy the yii\i18n\MessageFormatter code to a new file. Name it MessageFormatter.php and place somewhere in your application (but not in vendor folder).
In this new file change the format() method to:
public function format($pattern, $params, $language)
{
$this->_errorCode = 0;
$this->_errorMessage = '';
if ($params === []) {
return $pattern;
}
return $this->fallbackFormat($pattern, $params, $language);
}
Don't change anything else (including namespace).
Now let's use Yii mapping.
Find a place in your application when you can put code that will be run every time in bootstrapping phase. Good place for this is common/config/bootstrap.php if you are using "Advanced Template"-like project.
Add there this line:
Yii::$classMap['yii\i18n\MessageFormatter'] = 'path/to/your/MessageFormatter.php';
Obviously change the path to the one you've chosen. Now Yii autoloader will load this class from your file instead of the original Yii vendor folder (as mentioned in Class Autoloading section of the Guide).
In the modified file MessageFormatter method presence of intl library is never checked so fallback is used as default.
The downside of this trick is that you need to update manually your file every time original Yii file is changed (so almost every time you upgrade Yii version).
Another approach is to configure I18N component in your application to use your custom MessageFormatter where you can extend the original file and just override format() method inside without modifying class map.

Can I use the Blade templating engine outside of Laravel?

i'm want to creating a design pattern and use the "Blade templating engine".
Can I use the Blade templating engine outside of Laravel and use it in my new pattern ?
For the record:
I tested many libraries to run blade outside Laravel (that i don't use) and most are poor hacks of the original library that simply copied and pasted the code and removed some dependencies yet it retains a lot of dependencies of Laravel.
So I created (for a project) an alternative for blade that its free (MIT license, i.e. close source/private code is OK) in a single file and without a single dependency of an external library. You could download the class and start using it, or you could install via composer.
https://github.com/EFTEC/BladeOne
https://packagist.org/packages/eftec/bladeone
It's 100% compatible without the Laravel's own features (extensions).
How it works:
<?php
include "lib/BladeOne/BladeOne.php";
use eftec\bladeone;
$views = __DIR__ . '/views'; // folder where is located the templates
$compiledFolder = __DIR__ . '/compiled';
$blade=new bladeone\BladeOne($views,$compiledFolder);
echo $blade->run("Test.hello", ["name" => "hola mundo"]);
?>
Another alternative is to use twig but I tested it and I don't like it. I like the syntax of Laravel that its close to ASP.NET MVC Razor.
Edit: To this date (July 2018), it's practically the only template system that supports the new features of Blade 5.6 without Laravel. ;-)
You certainly can, there are lots of standalone blade options on packagist, as long as you are comfortable with composer then there should be no issue, this one looks pretty interesting due to having a really high percentage of stars compared to downloads.
Be warned though i have not tried it myself, like you i was looking for a standalone option for my own project and came across it, i will be giving it a real good workout though at sometime in the near future,
Matt Stauffer has created a whole repository showing you how you can use various Illuminate components directly outside of Laravel. I would recommend following his example and looking at his source code.
https://github.com/mattstauffer/Torch
Here is the index.php of using Laravel Views outside of Laravel
https://github.com/mattstauffer/Torch/blob/master/components/view/index.php
You can write a custom wrapper around it so that you can call it like Laravel
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Engines\CompilerEngine;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\Engines\PhpEngine;
use Illuminate\View\Factory;
use Illuminate\View\FileViewFinder;
function view($viewName, $templateData)
{
// Configuration
// Note that you can set several directories where your templates are located
$pathsToTemplates = [__DIR__ . '/templates'];
$pathToCompiledTemplates = __DIR__ . '/compiled';
// Dependencies
$filesystem = new Filesystem;
$eventDispatcher = new Dispatcher(new Container);
// Create View Factory capable of rendering PHP and Blade templates
$viewResolver = new EngineResolver;
$bladeCompiler = new BladeCompiler($filesystem, $pathToCompiledTemplates);
$viewResolver->register('blade', function () use ($bladeCompiler) {
return new CompilerEngine($bladeCompiler);
});
$viewResolver->register('php', function () {
return new PhpEngine;
});
$viewFinder = new FileViewFinder($filesystem, $pathsToTemplates);
$viewFactory = new Factory($viewResolver, $viewFinder, $eventDispatcher);
// Render template
return $viewFactory->make($viewName, $templateData)->render();
}
You can then call this using the following
view('view.name', ['title' => 'Title', 'text' => 'This is text']);
Yes you can use it where ever you like. Just install one of the the many packages available on composer for it.
If you're interested in integrating it with codeigniter I have a blog post here outlining the process.
Following the above steps should make it obvious how to include it into any framework.

The blade template engine can be used with codeigniter?

The template engine called blade can be used with codeigniter or pure php? I know that it can be used with laravel and I'd like to know if also can be used with any other php framework or with pure php
Blade can be used stand-alone in PHP.
This means you can comfortably use it in CodeIgniter.
https://github.com/PhiloNL/Laravel-Blade
Then again, you will need composer for that.
Alternatively you could use this CodeIgniter Library to simulate Blade: CodeIgniter Slice-Libray
It works pretty like Blade and it was designed directly for CodeIgniter!
For the record and as answer to another post:
I tested many libraries to run blade outside Laravel (that i don't use) and most (with all respect of the coders) are poor hacks of the original library that simply copied and pasted the code and removed some dependencies yet it retains a lot of dependencies of Laravel.
I created an alternative for blade that its free (MIT license, i.e. close source/private code is OK) in a single file and without a single dependency of an external library. You could download the class and start using it, or you could install via composes (composer require eftec/bladeone). So even composer is optional.
https://github.com/EFTEC/BladeOne
https://packagist.org/packages/eftec/bladeone
Its 100% compatible sans the Laravel's own features (extensions).
You can use BladeView library for CI.
NB: I ported this library
class Welcome extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library("bladeview");
}
public function renderView(){
$data=array(
"name"=>"Jhon",
"age"=>21
);
$this->bladeview->render("test", $data);
}
public function renderString(){
$data=array(
"name"=>"Jhon",
"age"=>21
);
$string="Hello I'm \{{$name}}. My age is \{{$age}}";
$this->bladeview->render($string, $data,false);
}
}
then in view.blade.php you can render like you do in laravel blade.
Hello my name is {{$name}}. My Age is {{$age}}.
Output:
Hello my name is Jhon. My Age is 21.
I have done a full write up here: http://mstd.eu/index.php/2017/03/02/using-the-laravel-blade-templating-engine-in-codeigniter-3/
Basically, include the package with composer (you need to set CI up to use composer), then create a blade instance passing it your view and cache folder like so:
$blade = new BladeInstance(__DIR__ . "/../views", __DIR__ . "/../cache/views");
echo $blade->render("index");

Using Smarty 3, Code Igniter 2, and HMVC together with smarty's inheritance?

I am using Code Igniter, The HMVC library, and Smarty with this library.
Smarty is working fine by default, however if I try to use smarty's inheritance feature ( {extends file="master.tpl"}) then we run into an issue.
The extends feature does not look in the module views folder for the extended file (in the above's case master.tpl), instead it only looks in the application/views/ folder and throws an error if it cannot find it.
I could add APPPATH."modules/smartytest/views" to the $config['template_directory'] array in the smarty config file. but that throws an error for each item in the array it checks first for the file. filemtime(): stat failed for application/views/master.tpl
and that has the added issue of, if I have three modules all the the array and the modules all have a master.tpl then no matter what module I call the extend from it will load the first one found.
So, is there a way to get smarty's extend function to behave nicely with the HMVC modules?
Ah, found a working solution,
in My_Parser.php edit the block at line 30 so it reads:
// Modular Separation / Modular Extensions has been detected
if (method_exists( $this->CI->router, 'fetch_module' ))
{
$this->_module = $this->CI->router->fetch_module();
//add the current module view folder as a template directory
if ($this->_module !== '')
$this->CI->smarty->addTemplateDir(APPPATH."modules/".$this->_module.'/views');
}
The one drawback of this method is that smarty will look in your application/views folder before the module views folder. if someone knows a solution to that then it would be fantastic.
The problem is that CI is not checking error_reporting() returns 0, because Smarty is using the # control operator:
So add the line at the top of the function "_exception_handler":
if (error_reporting() == 0) return;
To the "Common.php" file in the "_exception_handler" function (line 469), or create your own function with the same name before calling "CodeIgniter.php" in the index.php file.
Best!

How do I load a module specific custom helper from a task in symfony?

I'm rendering a partial from inside a task in symfony 1.4.6...
$this->configuration = $this->createConfiguration(app, env, false);
$this->configuration->loadHelpers(array('Partial'));
$context = sfContext::createInstance($this->configuration);
$html = get_partial(partialName, params);
...Inside the partial there is a reference to a custom helper, which can't be referenced from the default context, so an exception is throw...
Unable to load "fooHelper.php" helper in: SF_ROOT_DIR/apps/frontend/lib/helper, SF_ROOT_DIR/lib/helper, SF_ROOT_DIR/lib/vendor/symfony/lib/helper.
...And attempting to load the custom helper from the task referencing the module name in loadHelpers doesn't seem to fix the issue either...
$this->configuration->loadHelpers(array('Partial'), moduleName);
The helper I am trying to load is specific to the module, I don't really want to move it to one of the default project helper directories listed in the exception above. Any help would be appreciated!
Please have a look at the content of the HelperHelper.php helper file.
use_helper() can be used in a partial, so I guess putting this piece of code on top of the partial would do the job:
$context = sfContext::getInstance();
$context->getConfiguration()->loadHelpers(array('Custom'), 'moduleName');

Categories