viewRenderer helper allows to render templates from the same controller's actions directory, is it possible to render template from another module?
Edit: actually I found renderBySpec method in ViewRenderer but it seems it doesn't consider module param while building path, is there any other solution besides this?
In my projects, when I need to setup a different layout than default one then in my init function of Controller I set the correct layout. Like this:
public function init()
{
$this->_helper->layout->setLayout('different-layout');
}
Hope this help you.
Related
i would ask You about proper way to change dynamicly the $twig->render path called from controller.
I want to create custom loader or event subscriber, which works like:
when i set parameter/param and call from controller
return $this->render('system/create.html.twig');
twig will check, if template in path
system/custom/variable_name/create.html.twig
exists, load it, if not, load normal path
system/create.html.twig
I can't find proper way to realize it, have not idea, what i should use, EventSubscriber(and how to Subscribe on render method), or just realize it with some custom Twig loader?
#EDIT
Problem solved! There is no way to do it "on the fly", so i extend AbstractController in custom Class, override render method and check in this method all conditions.
I was wondering if yii components are also supporting the theme feature? In my environment right now a component is only considering files within the component/views/ folder.
Now that I am also using themes it would be nice to tell the component to look for the view under the themes/themeName/ folder.
Using the method below I can work around this but it certainly doesn't feel like this is the yii-way to do it.
protected function renderContent()
{
$view = './../../../themes/'.Yii::app()->theme->name.'/views/viewName';
$this->render($view);
}
Do you know of a more elegant solution to achieve this?
There isn't any theming on components. Mainly because they're not intended to be rendering content for anything. Nothing wrong with that though, sometimes it's required.
Easiest solution is probably to just make it more readable, using path aliases always helps:
protected function renderContent()
{
$view = 'webroot.themes.'.Yii::app()->theme->name.'.views.viewName';
$this->render($view);
}
Or you could add a method the component, or extend CComponent to get it across all components if you want it:
public function getViewsPath(){
return 'webroot.themes.'.Yii::app()->theme->name.'.views';
}
Or you could set a path alias:
Yii::setPathOfAlias('theme','webroot.themes.'.Yii::app()->theme->name);
Then you could use that anywhere in your application provided you run it at an early enough point in the process.
I had to create method in Boostrap which bootstraps Layout resource and registers some view helpers.
protected function _initViewHelpers() {
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->registerHelper(new Application_View_Helper_LoadMenu, 'loadMenu');
$view->registerHelper(new Application_View_Helper_InfoLink, 'infoLink');
$view->registerHelper(new Application_View_Helper_InfoData, 'infoData');
}
Now, I am passing some variables to layout (to Zend_View instance, as always), but layout doesn't recognize that it has them.
When I move code which registers helpers, to init() method in controller, everything is ok. Is it ZF error or I did sth wrong?
In Your Controller (or wherever you have view)
$view->layout()->some_var = "Some Value";
In Your Layout
<?php echo $this->layout()->some_var; ?>
if i'm missing some part of your question let me know.
Edit: failing the above, the other correct way to do this would be to use the placeholder helper (http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.placeholder)
Edit 2: Make sure you are bootstrapping view as well.
$this->bootstrap('view');
$view = $this->getResource('view');
Do your view-helpers implement the setView() method, either directly or perhaps as subclasses of Zend_View_Helper_Abstract?
If you look at the code for Zend_View_Abstract::registerHelper($helper, $name) method, you will see that it checks for the presence of a setView() method on the helper. If it finds such a method, then it calls $helper->setView($this), where $this is the $view.
This is where the connection takes place. In the absence of this call, it seems that although the view will be aware of the helper (after all, you did just register it), the helper will be unaware of the view. If the helper tries to access the view, it ends up creating a new view object, which is not the one you configured way back in Bootstrap.
tl;dr: There is probably no need to explicitly register the helpers. With the default resource-autoloader in place and the class/method naming convention you seem to be using, you can probably allow the built-in plugin-loader to handle all the instantiation. Simply call $this->myHelperMethod() in your layouts or view-scripts and all should be cool.
I am using the zend framework and trying to create and render a view from inside a controller. Normally this process is handled by the framework but I thought I could do it myself too as this part of the documentation states.
Unfortunately there is something wrong as the framework is still trying to load the default view as well. Here's my controller
<?php
class ViewController extends Zend_Controller_Action {
private $viewsFolder = null;
public function init()
{
$this->viewsFolder = realpath(dirname(__FILE__)) . '/../views/custom/';
}
public function indexAction()
{
// using a custom view (initialization and rendering executed by hand)
$view = new Zend_View();
$view->setScriptPath($this->viewsFolder);
$view->assign(array(
"dev_name" => "Fabs",
"framework" => "Zend frmwrk"
));
echo $view->render('customView.phtml');
}
}
and here is the error I get
Message: script 'view/index.phtml' not found in path (/home/ftestolin/stuff/rubrica/application/views/scripts/)
It looks like the normal view rendering cannot be suppressed. Any idea how to do it?
Probably better to disable the ViewRenderer rather than remove it. In controller:
$this->_helper->viewRenderer->setNoRender(true);
Remember that the ViewRenderer is where Zend_Form instances pull their default view for their own rendering. Removing the ViewRenderer means that it has to be re-instantiated later when the form needs to render. But when it does so, it recreates a brand new Zend_View instance. Any settings you have applied to your view - say, at bootstrap, setting doctype, etc - will be lost.
ok I will answer myself :)
one has to prevent the normal behaviour which is handled by Zend_Controller_Action_Helper_ViewRenderer
you can do it like that in the controller and then do your View instantiation business.
$this->_helper->removeHelper('viewRenderer'); // stop the default views rendering process
cheers
I've written a small MVC in PHP5 and desire a pagination module to be added to some files in my views section/folder..
I was wondering.. would the Pagination class be included in the Controller or Models section/folder?
Currently i've included it in the Models folder and called the function when needed..
The way I see it, pagination is a control, allowing user to tell your database (model), which portion of data he or she wants to see.
So I would go with the Controllers module.
Well, I think a better approach would be to make a helpers folder and then load them into your application like this :
function use_helper()
{
static $helpers = array();
foreach (func_get_args() as $helper)
{
if (in_array($helper, $helpers)) continue;
$helper_file = HELPER_PATH.DIRECTORY_SEPARATOR.$helper.'.php';
if (!file_exists($helper_file))
throw new Exception("Helper file '{$helper}' not found!");
include $helper_file;
$helpers[] = $helper;
}
}
Then all you have to do is build a pagination.php file with your Pagination class.
When you need it, you call the function
use_helper('pagination');
From here of course it depends on you Pagination class.
Hope this helps.
i guess the best approach is to call the pagination from the view, referring to this MVC
A view queries the model in order to generate an appropriate user interface
(for example the view lists the shopping cart's contents).
The view gets its own data from the model.
In some implementations, the controller may issue a general instruction to the view to render itself.
In others, the view is automatically notified by the model of changes in state (Observer) that require a screen update.
and because you will be using this class almost in every view, you should make a helper and include this class inside that helper so that all the views can share its methods