views:

612

answers:

2

Hi all,

I have a controller that is called with AJAX (sends JSON data), so I don't use a view.

I need to use a personnal view helper to format my data, but in my controller.

Is that possible ?

Or maybe I am doing it wrong (maybe I should have a view, but how with JSON) ?

+4  A: 

You can access any ViewHelper from the Controller by

$this->view->helpername(/*params*/);
// or
$helper = $this->view->getHelper('helpername');
// or
$broker = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$broker->getView()->helpername(/*params*/);

See http://stackoverflow.com/questions/2353034

However, you might be right that you are doing it wrong (funny pic btw), but I cannot really tell from your question. Please refine it as to why you need to call the view helper and what it is supposed to format.

Gordon
Yes how could I have forget that ! I feel stupid, but thank you.For the "is that right doing so" question : the view helper is there to format dates, numbers... How could I do differently ? (I can't do that with Javascript)
Matthieu
@Matthieu Ah, I see. I'd say ViewHelper sounds ok in this case then.
Gordon
A: 

Just use action helpers, many of view helpers are available as action helpers too.

Or directly by using Zend_Date or sprintf.

takeshin
I said "I need to use a *personnal* view helper"
Matthieu
Yes but I care about the Zend Framework architecture and I didn't spend months understanding it and creating view helper to tear it away by overriding the "helper" concept and call classes directly, or even worse : sprintf to format my numbers and dates.
Matthieu
You also said, `I don't use a view`. You meant you don't use `Zend_View`, or `.phtml` view scripts? $this->view->_helper; is obvious… Who knows what `personnal view helper` is :)
takeshin