Posts Tagged kohana
Setting up dynamic themes on Kohana
Posted by visual77 in programming on April 14th, 2010
It’s been a little over six months since I first began to use Kohana framework for all PHP development, and I thoroughly love the system. The only issue I’ve had with Kohana is the lack of built in, intuitive support for dynamic themes. I was unable to find an easy method to set up a theme that automatically wraps itself around all pages, while remaining dynamic enough to change certain elements on a page to page basis.
Over the past few days, I have been exploring methods to do this, with the following requirements:
- Keep all theme files separate from the application
- Require no changes to existing controllers and views to implement or change a theme
- Allow multiple applications on the same Kohana build to use the same theme
- Provide support for cascading themes
After a few failed attempts, I have found a solution that covers all four requirements and is pretty easy to implement. Of course, it is easier to implement this from the start, but if you have an existing Kohana site, it shouldn’t be too difficult to integrate this theming setup into the site. The concept of the theme is simple: create a theme as a module and then use hooks to implement the theme elements as appropriate.
How to flatten an array in PHP
Posted by visual77 in programming on March 23rd, 2010
In a recent Kohana project, I came across a somewhat odd PHP scenario – I had a multidimensional array that I needed to compress to a single dimensional array, but retaining all of the non array values. Basically, I need to make this change:
Array
(
to => Array
(
0 => stevep
1 => bobw
)
bcc => Array
(
0 => paulj
)
)
Array
(
0 => stevep
1 => bobw
2 => paulj
)
I looked around a bit for an existing PHP snippet to do just this, but they were all overly complex and used recursive callback functions. I had a feeling I could pull this off in a much more clean fashion. I toyed around a bit with the array functions and the ArrayObject library, and this is what I settled with:
$new_array = new ArrayObject(); array_walk_recursive($old_array, array($new_array, 'offsetSet')); $flattened_array = array_keys($new_array->getArrayCopy());
The big drawback is the way ArrayObect::offsetSet() accepts its parameters, compared to how array_walk_recursive() sends the callback parameters; they are reversed. This causes the values to become keys, which has the effect of canceling out duplicate values. If your script needs duplicate values to remain intact, then this solution won’t work for you. Otherwise, it should work out just fine.