Change default template to 'common.volt'? - php

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

Related

"Action" in indexController not rendering in view

I have an existing Zend 1.12 project with modules et al that I am trying to simply add a view to create another web page. So there is a controller called "Management" that has an index action, the default of course, and the correct view page called index.phtml. That is all fine and works inside the entire project/website.
class Management_IndexController extends Default_Controller_Action {
function indexAction() {
$this->view->headStyle()->appendFile('contact.css', 'contact')
->appendFile('message.css')
;
$this->setLayout('management');
$this->view->headTitle()->set('Management');
$message = new Com_Ui_Message();
$request = $this->_request;
$settings=new Com_Data();
$content=new Com_Data();
$data = new Com_Data($request->getPost());
$this->setFocus('#name');
}
function chatAction() {
$this->view->headStyle()->appendFile('contact.css', 'contact')
->appendFile('message.css')
;
$this->view->testMessage = "test";
}
}
So if I browse to http://www.bmcmusicgroup.com/management I see the default action public function, and that is all good. If I try to add another public function called chatAction inside of the management controller, and create a view page called chat.phtml in the appropriate folder where the index.phtml file is (the management index.phtml file), and then point my browser to http://www.bmcmusicgroup.com/management/chat I get nothing.
This is an existing site that I am trying decipher. Any pointers would be greatly appreciated.
Try changing your url to the following and you should see the page: http://www.bmcmusicgroup.com/management/index/chat. The URL you entered (http://www.bmcmusicgroup.com/management/chat) decomposes like so:
Module: management;
Controller: chat;
Action: index

I'm trying to load a custom layout, it loads the layout but not the module

I was looking at a few answers here but just one did it. (Load a custom layout in a module).
I've tried with:
$this->layout('layout/something');
But it didn't worked then i saw it:
$viewModel = new ViewModel();
$viewModel->setTemplate('layout/something');
return $viewModel;
I have this in the module config:
'template_map' => array(
'layout/something'=> __DIR__.'/../view/layout/something_layout.twig',
),
Then when i browse this, it prints the template layout but not the module's view.
Any help?
UPDATE:
I've found the error. This is because i've installed ZfcTwig and this module basically ignores the layouts of ZF2, i have to extend (in the view file) to the layout that i'm going to use. But the problem of it is that this ignores the ZF2 layouts.
You need to use the Layout controller plugin to switch layout:
// A controller's action method that uses an alternative
// layout template.
public function indexAction() {
//...
// Use the Layout plugin to access the ViewModel
// object associated with layout template.
$this->layout()->setTemplate('layout/layout2');
//...
}

Zend Framework 2 display a view within a view

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);

view script doesn't recognizing view helpers in zend framework

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

disable layout for sfDoctrineGuardPlugin in Symfony

How could I prevent mentioned plugin's login form from using default layout? I am aware of this question, but that answer doesnt work for me. For starters, there's no signin module in modules dir, probably plugins handle it in different way, I dont know. Just learning symfony. Thanks in advance :)
For now its not possible to set custom layout for some sfGuardAuth action via custom view.yml.
This is how I did it.
This is my apps/backend/modules/sfGuardAuth/actions/actions.class.php:
<?php
require_once(sfConfig::get('sf_plugins_dir').'/sfDoctrineGuardPlugin/modules/sfGuardAuth/lib/BasesfGuardAuthActions.class.php');
class sfGuardAuthActions extends BasesfGuardAuthActions
{
public function preExecute()
{
$layout = $this->getActionName() == sfConfig::get('sf_login_action') ? 'sfGuardLayout' : $this->getLayout();
$this->setLayout($layout);
}
}
If you just want to set a different layout, you need to add a module (just create it manually) called "sfGuardAuth". Inside the /config/ directory for that, change the layout in the view.yml like for any other module. This is explained in:
http://www.symfony-project.org/plugins/sfDoctrineGuardPlugin/4_0_0
... under section "Customize sfGuardAuth module actions".
However, if you want to "embed" your login form on another existing page, you could turn the login into a component - which means it uses the existing layout of the page it occurs in.
Component action in a custom module:
public function executeSigninLightbox(sfWebRequest $request)
{
$class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin');
$this->form = new $class();
}
... which like all components uses a partial as its view. The partial now has access to $form like a standard login page. The partial for this would be called "_signinLightbox".
Hope that helps.

Categories