I just want to display a Excel file existing on my hardisk in a twig view.
I'm using Symfony[2.5] and "Liuggio/Excelbundle"
This trick works well but i want to add it in my view.
public function newAction()
{
$filename = 'filename.xlsx';
$reader = \PHPExcel_IOFactory::createReaderForFile($filename);
$excel = $reader->load($filename);
$writer = \PHPExcel_IOFactory::createWriter($excel, "HTML");
$writer->generateStyles();
$writer->generateSheetData();
// this doesnt work..
return $this->render('MonextReportingBundle:Default:excel.html.twig', array(
'excelHtml'=>$writer
));
And in my excel.html.twig :
{{ excelHtml | raw }}
Catchable fatal error: Object of class PHPExcel_Writer_HTML could not be converted to string
Thanks a lot guys ! Excuse my english..
PHPExcel's HTML writer has no toString(), that's why your attempt didn't work.
It has, however, a method called generateSheetData that seems to do what you want. Use it like this:
{{ excelHtml.generateSheetData | raw }}
Related
Currently using the SlimPHP 3 MVC framework and trying to XML output of a URL to the Twig template. I'm using the latest build of Guzzle if it helps.
I have my Controller set up and the function as follows:
public function getaws($request, $response, $args) {
$bucketname = $args['bucketname'];
$client = new \GuzzleHttp\Client();
$bucket = $client->request('GET', $bucketname . '.s3.amazonaws.com');
if($bucket) {
$buck = $bucket->getBody();
return $this->view->render($response, 'aws.twig', ['buck' => $buck]);
}
else {
$res = Array();
$res['message'] = 'Bucket Disabled or Unknown';
return $this->view->render($response, 'aws.twig', ['buck' => $res]);
}
}
In the Twig file - aws.twig I have only been able to output the entire XML response/body using:
{{ buck }}
I have tried typing:
{{ buck.Name }}
or
{{ buck.Key }}
Neither have worked. I've tried the ForEach loop as well and nothing comes from that at all. I'm completely at a loss. I've noticed that there doesn't seem to be an awful lot of help on SlimPHP but I like it and prefer using it to Laravel and no I won't switch to Laravel just to make life easier otherwise asking for help with this would be a waste of time.
Anyone who has had experience with SlimPHP 3, Twig templates and Guzzle - I'd very much like help with this.
Thanks!
I'm trying to show an Advanced Custom Fields repeater in custom order in a Timber/Twig based WordPress theme. Is it possible to install the array-extension (http://twig-extensions.readthedocs.io/en/latest/array.html) to achieve this or how can it be done? I'm completely lost at the moment and would appreciate any ideas on how to solve it.
I wouldn't use a twig extension.
Why not something like this:
$rows = get_field( 'repeater_field' );
if( $rows ) {
shuffle( $rows );
foreach( $rows as $row ) {
// your code
}
}
In your php file (index.php, page.php etc.)
// Create context
$context = Timber::get_context();
// Get Field
$repeat = get_field('my_repeater_field');
// Shuffle it
shuffle($repeat);
// Add it to context
$context['repeat'] = $repeat;
And then call it in your twig file as your normally would
Using Timber, you can also do this with the Twig sort filter:
{% for item in items|sort(rand) %}
https://twig.symfony.com/doc/2.x/filters/sort.html
I was trying to get xml code into string. Here is an example:
URL: .../importarxml?enviosXml=<?xml version="1.0" encoding="iso-8859-1"?>+<lista>+<envio>+<REFERENCIA>XXXXXX</REFERENCIA>+<CODIGO>XXXXX</CODIGO>+<CLIENTE>OCAÑA</CLIENTE>+<FCARGA>13/12/2013</FCARGA>+<LCARGA>XXX</LCARGA>+<DESTINATARIO>XXXXX</DESTINATARIO>+<FENTREGA>18/12/2013</FENTREGA>+<LENTREGA>PARIS</LENTREGA>+<MATRICULA>XXXXXX</MATRICULA>+<BULTOS>2</BULTOS>+<PESO>1302</PESO>+<OBSERVACIONES></OBSERVACIONES>+</envio>+</lista>
That's my code to get parameters from URL in my Controller in symfony:
class EnviosController extends Controller{
public function importarxmlAction(){
$request = $this->getRequest();
$em = $this->getDoctrine()->getManager();
$xml = $request->query->get('enviosXml'); // $xml get bad characters
ld($xml); --> (Displays $xml string content. Show bad characters)
//For example 'OCAÑA'-->'OCA�A'
(ldd-->LadybugBundle)
$utf8_1 = utf8_decode($xml);
$utf8_2 = iconv('UTF-8', 'ISO-8859-1', $utf8_1);
$utf8_2 = mb_convert_encoding($utf8_2, 'ISO-8859-1','UTF-8');
ld($utf8_2); // Display $utf8_2 content
...
}
}
I tryed utf8_decode(),iconv() and mb_convert_encoding() but didn't help me. I don't know what else to do, if anyone could help me I'll appreciate it.
Thanks.
P.S: Sorry for my bad english!
It's not a good idea to pass a whole XML in the url, by the GET method. Your url is not even escaped to be used as url string...
You should either give the reference of an existing file by its id/name or upload it by a form (method POST)
Then check the encoding of your file.
Hi I am new to Symfony2 and I need to upload Excel file to MYSQL database?
Can anyone send me a example of how to do it?
Thanks you
Raki
Start by adding: "CodePlex/PHPExcel": "1.7.7", to your composer.json file and updating.
Add a class to sit between the PHPExcel stuff and your code. Something like:
namespace Cerad\ArbiterBundle\Format;
class Excel
{
protected function createReaderForFile($fileName,$readDataOnly = true)
{
// Most common case
$reader = new \PHPExcel_Reader_Excel5();
$reader->setReadDataOnly($readDataOnly);
if ($reader->canRead($fileName)) return $reader;
// Make sure have zip archive
if (class_exists('ZipArchive'))
{
$reader = new \PHPExcel_Reader_Excel2007();
$reader->setReadDataOnly($readDataOnly);
if ($reader->canRead($fileName)) return $reader;
}
// Note that csv does not actually check for a csv file
$reader = new \PHPExcel_Reader_CSV();
if ($reader->canRead($fileName)) return $reader;
throw new Exception("No Reader found for $fileName");
}
public function load($fileName, $readDataOnly = true)
{
$reader = $this->createReaderForFile($fileName,$readDataOnly);
return $reader->load($fileName);
}
}
Now in your code you would have something like:
$excel = new Excel();
$reader = $excel->load('SomeFileName.xls');
$ws = $reader->getSheet(0);
$rows = $ws->toArray();
And process away.
the example of Cerad is an option but it doesn't use that bundle and neither work with mysql,
that bundle is a simple dependency that expose services,
If you have to store data from excel to mysql,
you need first do upload file somewhere, then
read the file with the service,
$excelObj = $this->get('xls.load_xls5')->load($filename);
and then you have to read the documentation of PHPExcel
Hope it helps a little
I'm learning Kohana 3.2.0 together with KSmarty for Kohana 3. I'd like to write an anchor on the page like this:
Page list
I can build the url in the controller and pass it to Smarty as a variable but. Is there a way to build the anchor or URL in Smarty template (including "http://www.mysite.cz" part)?
If it is not possible to build the anchor. Is it at least possible to build full URL?
The Reason: I have a main template which includes another template.
The main template will be used by multiple controllers and I would like to avoid building the URL in each controller. Therefore I'll be happy if KSmarty will be able to do it for me.
The only solution I have found is to write custom function. Save following code into function.url.php file in Smarty plugins directory:
function smarty_function_url($params, &$smarty)
{
$type = '';
if(isset($params['type'])) $type = $params['type'];
$protocol = 'http';
if(isset($params['protocol'])) $protocol = $params['protocol'];
$url = '';
if(isset($params['url'])) $url = $params['url'];
$text = '';
if(isset($params['text'])) $text = $params['text'];
switch($params['type'])
{
case 'url':
return Kohana_URL::site($url, $protocol);
case 'anchor':
$url = Kohana_URL::site($url, $protocol);
return "<a href='{$url}'>{$text}</a>";
default:
return Kohana_URL::base('http');
}
}
Examples of use in Smarty template:
{url}
{url type='url' url='admin/categories' protocol='https'}
{url type='anchor' url='admin/articles' text='List of articles'}
The first block in which variables are set I had to write otherwise Smarty was generating notice "Undefined variable...". I'm just PHP student, suggestions for code improvement are welcome.
Hope it will help the others.