I have one java script definition in view(.phtml) file. This java script has dynamic parameters like controller name and action name. To avoid duplicate code i want to put this code in helper file.
Now is it possible that i can use pure html and javascript code in helper file? If yes then how can i call that helper in my view file?
Or Is there any other best way to do this?
Thank you…
A view helper would not be the solution in this instance. More likely you will want to use a partial (which is a view helper). A view helper is something normally used to
returna piece of data to the view. Apartial()orpartialLoop()is used to display common html in the view (pieces of html that can be reused in multiple views).Here is an example of a simple view helper, notice it returns a result:
Now a partial will contain Html (I’m petty sure JS will work as well)
Here is a partial that uses view helpers
call this partial in your view script, the first argument is the path of the partial
the second argument is the data used in the partial
the data is sent from the controller action as usual
$this->view->memberData = $memberDataThis is common usage but not the only way to get data to the partial.
in this example the partial is at the default location
/application/views/scriptsHope this helps