Tag Archives: layout

Zend Framework: How to Build a Dynamic Layout by Setting Variables in your Controllers CodeUnit 04 APR 2011

I have a Zend Framework website that is broken up into modules. Accordingly, I want my single layout.phtml Layout script to dynamically change ever so slightly depending on what module or rather controller I hit – in my case change the header image. So how do we do this?

Well the first thing is gaining access to the Layout object so that you can set some variables in this.

To gain access within action controllers, use the layout() action helper:

// Calling helper as a method of the helper broker:
$helper = $this->_helper->getHelper('Layout');
$layout = $helper->getLayoutInstance();

To gain access within view scripts, use the layout() view helper:

$layout = $this->layout();

To gain access via the bootstrap, retrieve the layout resource:

$layout = $bootstrap->getResource('Layout');

Finally if you still can’t get a layout object to work with, use the static method (not entirely recommended):

$layout = Zend_Layout::getMvcInstance();

Now that you have a $layout object to work with, you’ll probably want to assign some sort of variable value to it in order to dynamically affect your layout.phtml file. To set a variable you can either directly assign with the = operator or make use of the assign() method:

// Setting content:
$layout->somekey = "foo"
 
// Using the assign() method:
$layout->assign('someotherkey', 'bar');

Okay, now that we have a variable stored in the layout object, it becomes trivial to make our layout dynamic. For example, having previously added a value to $layout->mainheaderlogo = ‘image1.png’, we can now add the following to our layout.phtml file:

<div id=”logobanner”><a href=”/”><img src=”/images/<?php echo $this->layout()->mainheaderlogo; ?>” /></a></div>

And now you know. Nifty.

(Update: Or you should probably really learn to use Zend View Placeholders correctly, as they are the correct way of making your layout more Dynamic!!)

Setting Your Worksheet Printing Layout Options in PHPExcel CodeUnit 18 APR 2010

I’ve gone on and on in length over just how awesome the PHPExcel OpenXML spreadsheet reader/writer project is over the last bit here in this blog, and today is no exception as I take a quick look at how one can set the page printing layout when generating an Excel spreadsheet.

Now we all know how annoying it is when we right-click on an Excel spreadsheet and select print, only to recoil in horror when we realise that the columns don’t exactly fit nicely on a single page, meaning a spill-over effect and thus a lot of wasted paper!

So ideally what one wants to do for a spreadsheet is preset the document’s printing preferences in order to avoid handing over this quick print annoyance to our spreadsheet users.

Naturally, being as awesome a system as what it is, PHPExcel comes bundled with a number of methods that allow us to specify page size/type and orientation, as well as scaling options.

For this example, we’re going to set our spreadsheet to print in A4 size with a portrait layout and scaled so that it fits to 1 page wide and infinite pages tall.

// Redirect output to a client’s web browser (Excel5)
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=demo.xls");
header('Cache-Control: max-age=0');

// PHPExcel
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set Orientation, size and scaling
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToPage(true);
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToWidth(1);
$objPHPExcel->getActiveSheet()->getPageSetup()->setFitToHeight(0);

// Generate spreadsheet
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

As you can see above, setting the various options are pretty self explanatory. There are a number or preset orientation and papersizes to choose from (see ClassesPHPExcelWorksheetPageSetup.php for the options) and then the main full page scaling option is dealt with by the setFitToPage boolean flag. If set to true, you can then specify by how many pages wide you wish to force the layout and also how many pages tall you want it. Specifying a 0 means infinite pages.

If however you set FitToPage to false but still want to scale, you can make use of the setScale() function which allows you to set the percentage scaling you wish to apply.

And that’s precisely how simple all of this is!

Related Link: http://phpexcel.codeplex.com/

The New CodeUnit Skin CodeUnit 21 MAR 2010

Alright boys and girls, it is time to hit that good old Ctrl+F5 to force a cache refresh of http://www.craiglotter.co.za once more. As well aware as I am that there are far better and more constructive ways in which to spend an whole Saturday afternoon at home in front of the PC, it turns out that the lure of churning out something special is just that much stronger than what I thought and so I slaved away yesterday with scant regard for time in order to present you with what you now see in front of you:

The totally new look CodeUnit of Craig website.

I’ve added and fiddled with some functionality, made a few tweaks, increased site advertising and most of all, ran an extensive overhaul in terms of graphics and layout on the site, the end result being something that turns out to be quite… pleasing to my eye for a change.

I like it, and I hope you do to.

(Still, ever the perfectionist there are still some small niggles I want to iron out, but those I’ll get to in the background… you know, when I don’t feel like doing household chores or something like that! :P)

The Coricraft has Landed My Life 16 FEB 2010

If any of you follow either Chantelle or myself on Facebook then you’ll know that last week Chantelle’s long awaited Coricraft couches finally reached us courtesy of the Epping-based factory’s delivery truck, and after an effortless carry through the handy sliding door, our two new oversized babies were sitting snuggly in the middle of our lounge floor.

However, as we suspected, the size of our new addition to the Nagua Bay household proved to be the source of quite a big headache. Too big to fit in our usual configuration, we then had to spend the first night pretty much moving around the whole household to try and come up with a layout that a) actually worked and b) we liked.

Needless to say, this was a lot harder than what it sounds, particularly when you started involving the various odds and ends of furniture and belonging that kept having to be shifted about with each new configuration try-out. But manage we did, and eventually us two grumpy bears found a configuration that fitted and didn’t look half bad, leaving the hard work of getting the place all wired up and neat and tidy again for the next evening – which I dutifully handled by myself for the most part as poor old Chantelle had a late shift to deal with that day! (Convenient eh? :P)

Oh my poor back I tell you!

But as you can see, the flat is back to its fabulous self, the entertainment section has been re-wired and organised, plants and goodies are all in their new places and Chantelle is now back where she belongs – firmly plonked down in a continuous state of sleep, cuddled by love bug and with a cold cup of coffee that I lovingly prepared in front of her.

Needless to say she’s in Nirvana. (And as the guys back in the office can testify to, very VERY excited about our latest acquisition!)

I on the other hand need to get my short legs used to those giant puffy cushions all over again…

(Oh, and if you are wondering why the heck such a light colour if we have furry cats bounding about… blame Chantelle – but hey, I guess that’s what Scotchguard was intended for in the first place! :P)