Lorem ipsum view helper
by Naneau
A while ago I wrote a little lorem ipsum class. I use it a lot when creating mock-ups. Having realistic filler text really helps with visualizing things.
When I wrote about view helpers the other day it dawned on me that I’ve never posted the view helper I created for use with the lorem ipsum class. It’s the perfect thing to wrap in a view helper. It’s something you’d use in your views, and you don’t want to write the code to use the class directly into your views. Really, you don’t
Anyway, here it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php /** * LoremIpsum.php * * @category Naneau * @package Naneau_View_Helper * @copyright Copyright (c) 2007 Maurice Fonk - http://naneau.nl * @version 0.1 */ require_once 'Naneau/Text/Filler.php'; /** * Naneau_View_Helper_LoremIpsum * * get a number of lorem ipsum paragraphs (random filler text) * * @category Naneau * @package Naneau_View_Helper * @copyright Copyright (c) 2007 Maurice Fonk - http://naneau.nl */ class Naneau_View_Helper_LoremIpsum { public function loremIpsum($count = 2, $html = true) { return Naneau_Text_Filler::getParagraphs($count, $html); } } |
Use it like this (in your view scripts):
1 | <?php echo $this->loremIpsum(5); /* 5 paragraphs */ ?> |
You have to have the original class in your library, though.
Comments
Am I right if I suggere to add
$view->addHelperPath(‘/Naneau/View/Helper/’, ‘Naneau_View_Helper_’);
in the bootstrap to make this work ?
yup, if you put it in your library under /Naneau/View/Helper/LoremIpsum.php (like you should if you follow the coding standard), that is the way to make the view object find it.
Hold on, you can’t just add $view->addHelperPath(’/Naneau/View/Helper/’, ‘Naneau_View_Helper_’); to your bootstrap. You have to define $view first if you do that, correct?
Perhaps it would work better if you stuck in the controller’s init method with $this->view->addHelperPath(‘/foo/’,'foo’); ?
I would certainly suggest you set up your view’s helper paths in your bootstrap. The trick is using viewRenderer to modify the view object. You can retrieve it from the action helper as it’s a public property.
In your bootstrap you can create the action helper:
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
Then use it’s view object to do whatever you want (including setting values):
$viewRenderer->view->addHelperPath(’/Naneau/View/Helper/’, ‘Naneau_View_Helper_’);
When using this I get a STRICT warning about:
“Non-static method Mine_View_Helper_Filler::getParagraphs() should not be called statically”
How should it be called?
I’m confused about the viewRenderer. If I create the action helper in my bootstrap as you indicate, where would I call $viewRenderer->view->addHelperPath(’/Naneau/View/Helper/’, ‘Naneau_View_Helper_’);
If I put $viewRenderer->view->addHelperPath(’/Naneau/View/Helper/’, ‘Naneau_View_Helper_’); in my controller init, I get a “Notice: Undefined variable: viewRenderer” (but I declared it in the bootstrap? Didn’t I?)
If I put it in my bootstrap I get a “Call to a member function addHelperPath() on a non-object”
So… where does it go?
If you’re using Zend_Layout it’s simple. Bootstrap file:
…
$layout = new Zend_Layout(…);
$layout->getView()->addHelperPath(‘/Naneau/View/Helper/’, ‘Naneau_View_Helper’);
…