my problem is fairly when I call a view helper from view script it can't be called
although I added properly all information path to the config file via this line:
resources.view.helperPath.ZF_View_Helper_="ZF/View/Helper/"
also I registered the helper in bootstrap file
function _initViewHelpers(){
$view = new Zend_View();
$view->addHelperPath('ZF/View/Helper','ZF_View_Helper');
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
$viewRenderer->setView($view);
Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
}
but in vain it still printing out this error message:
Application error
Exception information:
Message: Plugin by name 'OutputHelper' was not found in the registry; used paths:
Zend_View_Helper_: Zend/View/Helper/
it doesn't include the custom view helper path as expected ;
the path of the view helper is: library/ZF/View/Helper/OutputHelper.php
can you do this:
in view script
$view = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('view');
var_dump($this === $view);
var_dump($view->getHelperPaths());
exit;
I think your view instance are replaced at some point.
May be module's bootstrap have view resource?
Or it can be other obvious mistake. So obvious so you'll never think of it
btw remove that _initViewHelpers method. Zend_Application_Resource_View work just fine for that.
And if you use this method, use it correctly, eg:
$this->bootstrap('view');
$view = $this->getResource('view');
//whatever
Related
For loading the views in CodeIgniter, I have to repeat loading the fixed views (header and footer) which is a little annoying to be repeated for every view-related controller.
Currently when I want to load views in CI, I do the following:
$this->load->view("header");
$this->load->view("index");
$this->load->view("footer");
Then, how can I change $this->load->view(); to get a parameter (for instance boolean) which allows a view to be loaded before/after the targeted view. Such as this one:
$this->load->view("index", TRUE, FALSE, $data); // TRUE=>header FALSE=>footer $data=>common variable
Is it possible to hack the function like this?
try this library, it worked for me, when I used it
https://github.com/philsturgeon/codeigniter-template
You can do with library.
Create a new library file called template.php and write a function called load_template. In that function, use above code.
public function load_template($view_file_name,$data_array=array())
{
$ci = &get_instatnce();
$ci->load->view("header");
$ci->load->view($view_file_name,$data_array);
$ci->> load->view("footer");
}
You have to load this library in autoload file in config folder. so you don't want to load in all controller.
You can this function like
$this->template->load_template("index");
If you want to pass date to view file, then you can send via $data_array
From what I understand, Phalcon uses index.phtml or index.volt in app/views as the base template for any page that doesn't have a template specified.
How can I change this to use app/views/layouts/common.volt?
It uses index.volt or index.html if the latest executed action is 'index' (indexAction in the controller).
You can use a common layout by setting a 'template before' or a 'template after':
https://github.com/phalcon/invo/blob/master/app/controllers/ContactController.php#L7
Update August 2016: since the above information is no longer available in the given link, adding it here:
public function initialize()
{
$this->view->setTemplateBefore('your-template-name');
$this->view->setTemplateAfter('your-template-name');
}
More info here: https://docs.phalconphp.com/en/latest/reference/views.html#using-templates
When setting up the view component we need to declare $view Object like the following:
$di->set('view', function () use ($config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->setLayout('common');
......
using setLayout(String name) method to set default layout for app
I have two modules Admin and Login.
I want to display the Login view 'login.phtml' within the admin view 'index.html'
I have the following in the Admin modules indexAction controller
public function indexAction()
{
$login = new LoginController();
$view = new ViewModel(array(
'theloginform' => $login->loginAction(),
));
return $view;
}
In the LoginAction method in the Login controller I return the ViewModel for the 'login.phtml' file.
public function LoginAction() {
$view = new ViewModel();
return $view;
}
The indexAction throws an error as the variable 'theloginform' is an object.
Catchable fatal error: Object of class Zend\View\Model\ViewModel could not be converted to string in...
If i add the following:
$authentication->loginAction()->captureTo('test')
The 'index.phtml' shows a string "content".
I have read that i may need to render the ViewModel before i assign it to the view variable 'theloginform', but i can't seem to get it to work, i have tried the following with no luck.
public function LoginAction() {
$view = new ViewModel();
$renderer = new PhpRenderer();
$resolver = new Resolver\AggregateResolver();
$map = new Resolver\TemplateMapResolver(array(
'login' => __DIR__ . '/../view/login.phtml'
));
$resolver->attach($map);
$view->setTemplate("login");
return $renderer->render($view);
}
If get the following error:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "login"; resolver could not resolve to a file
I have even tried adding the DI into the autoload_classmap.php file but still get the same error, i have double checked the login.phtml file is at the correct path:
'/Login/view/login/login/login.phtml' I even copied it to '/Login/src/Login/view/login.phtml'
Very confused have read then re-read the Zend documentation, i just want to pass a view to another view...
If you need share some view content you can use partials for that:
$this->partial('partial/login.pthml', array()); //add this to your index view
you can read about them here
You may also find some usefull information: How does Zend Framework 2 render partials inside a module?
As per this zf2 documentaion page
Write this in login Action:
public function loginAction()
{
return new ViewModel();
}
And in indexAction :
$view = new ViewModel(
array(
//here any thig you want to assign to index view
)
);
$loginView = new ViewModel(
array(
//here any thig you want to assign to login view
)
);
$loginView->setTemplate('moduleName/controllerName/login');
$view->addChild($loginView, 'login');
return $view
In index.phtml you can just echo login <? echo $this->login ?> where ever you want to display loginView.
In ZF 1.x I would likely recommend you build an action helper that is referenced to a view placeholder or a controller plugin that calls back to loginAction for the form logic.
In Zf2 it looks like action helpers have been replaced by controller plugins and seem to be triggered through the event manager and may need to be aware of one or more of the "managers". However the placeholder view helper still exists and even seems somewhat familiar.
I would suggest you look into building/adapting a controller plugin for your login form display that can then be attached to a placeholder view helper. You might be able to get the required functionality with just a view helper, if you're lucky.
I wish I could help more, but I'm still wading through this mess myself.
Good luck.
In you admin view you have to use the render view helper and echo the script rendered, so you can do echo $this->render($this->theloginform);
I want to add a view helper path in an existing project. To do this I have added the following line to my application.ini:
resources.view[] =
And in my bootstrap file:
$this->bootstrap("view");
$view = $this->getResource("view");
$view->addHelperPath(APPLICATION_PATH . "/../library/MyPath", MyNamespace");
Now I am indeed able to add view helpers to my path, so no problem there.
However, variables that I have added to the view in my Action Helpers are suddenly no longer accessible inside my views. I can retreive them inside my layout as usual so I know they get assigned properly.
I assign a variable in my Action Helper in the postDispatch:
$view = $this->getActionController()->view;
$view->myVar = $this->var;
Then in my layout
Zend_Debug::dump( $this->myVar );
results in: (string) "myVar contents"
And in my view
Zend_Debug::dump( $this->myVar );
results in: null
Since this is an existing project I need a general solution that I can use in either my bootstrap or application.ini
You can add paths to your view from application.ini:
resources.view.helperPath.MyPrefix = "/path/to/helpers"
Hopefully this will fix the problem. I'm assuming that the way you do it, an existing view-instance is somehow overwritten, which destroy previous changes. You could try fetching the view from the frontcontroller, set the helper-path, and pass it back to the frontcontroller.
After some research this is what I found out.
Edit: Removed text concerning differences between view variables and layout variables - see comments by #pieter and #david weinraub
The postDispatch of the Action Helper fires AFTER the view is rendered, but BEFORE the layout is rendered. Therefore, any variables assigned to the view in the postDispatch of the action helper were unavailable in my views, they didn't exist yet.
I am very curious why this behaviour occurs ONLY when actively bootstrapping the view? I will leave the question unanswered, maybe someone can clarify this.
I am trying to set up my zend route using the routes.ini and bootstrap but for some reason it is not able to route as expected. My routes.ini and bootstrap.php are as follows.
routes.ini
[production]
routes.guestbook.route = "/guestbook"
routes.guestbook.defaults.controller = guestbook
routes.guestbook.defaults.action = index
bootstrap.php
protected function _initRoutes()
{
// Get Front Controller Instance
$front = Zend_Controller_Front::getInstance();
// Get Router
$router = $front->getRouter();
$router->addConfig(new Zend_Config_Ini(APPLICATION_PATH.'/configs/routes.ini', 'production'), 'routes');
}
After I've read your comment, I can assert that you can delete those statements (config and bootstrap) because what you want to achieve is the normal behavior of the zend framework default router unless you're using modules.
Thanks to FloydThreepwood who remeber me to write this detail.
The easiest way to configure routing is by using the Zend_Application_Resource_Router.
Configuration goes in your application.ini file and that's it, no further code required.
As it appears you're using a static route (no variable path components), try this in your application.ini file
resources.router.routes.guestbook.type = "Zend_Controller_Router_Route_Static"
resources.router.routes.guestbook.route = "guestbook"
resources.router.routes.guestbook.defaults.module = "default"
resources.router.routes.guestbook.defaults.controller = "guestbook"
resources.router.routes.guestbook.defaults.action = "index"
Remove the _initRoutes() method from your Bootstrap class.
Also, this is just an aside but when using other resources such as the front controller in a bootstrap _init* method, you must ensure they've been properly bootstrapped. To do so, retrieve them like this
protected function _initSomething()
{
// make sure resource is bootstrapped
$this->bootstrap('frontController');
// retrieve resource
$front = $this->getResource('frontController');
}
See http://framework.zend.com/manual/en/zend.application.theory-of-operation.html#zend.application.theory-of-operation.bootstrap.dependency-tracking