Nested View in Theme Views using Fuelphp - fuelphp

I have working with Fuelphp and done some tiny apps, Now i am moving to bigger one, now i am stuck with this.
I have enabled theme in fuelphp and it is working perfectly, In my App there is a Top Nav bar, In Nav bar there is 3 drop down notification system like facebook & also search option. So i have created top_nav partial. I want to make the search and notification system in partial inside partial, to making it more modular, so i created another partial of top_nav_search and top_nav_notif . Both partials need some variables to transfer from controller, How i do that. My variables are passing to top_nav only. Not top_nav_search or top_nav_notif.
How can i add partial inside the partial.

Answer Found:
using View Class will not work with theme. It will search for APP/View folder.
we have to use
echo Theme::instance()->view('partials/partial_file')->set_safe('var', $this->get('var'));
found working

You can use set_global on your view, and then the exposed variable will be available everywhere.
$view->set_global('some_data', $some_data);
// instead of
$view->some_data = $some_data;
Also, when using set_partial, you can specify a View instance instead of the path for the view. That way you can pass variables directly into the View instance specified to set_partial. Though I'm not sure it solves your problem. Still, here's an example:
$view = View::forge('nav/search');
$view->some_data = $some_data;
$theme->set_partial('top_nav_search', $view);
Edit: I wrote View::forge, but more appropriate would've been to write $theme->view in this example. Like this:
$view = $theme->view('nav/search');
$view->some_data = $some_data;
$theme->set_partial('top_nav_search', $view);

Related

Call Modules Controller from Application Bootstrap

I've asked a question like this previously but I believe this is different (that one was just a general question).
I implemented Zend_Navigation.
For menu I used DB Table to store menu items and did recursion on Array-s to get the tree of menu items.
All of this action takes place in my module called Menu. Inside I have:
Menu --
Controllers --
IndexController.php
Models--
DbTable--
Menu.php
Bootstrap.php
inside index controller I have a function menuGenerator($menu_id)
So following tutorials on Zend_Navigation, the menu is initialized in the application bootstrap.
my function inside application's bootstrap looks like this:
public function _initMenus() {
$menuArray = new Menu_IndexController();
$outArray = $menuArray->menuGenerator(1);
$mainmenu = new Zend_Navigation($outArray);
$this->view->navigation($mainmenu);
}
and it gives me an error:
Fatal error: Class 'Menu_IndexController' not found in D:\Server\xampp\htdocs\project\application\Bootstrap.php on line 8
So, Any ideas how should I make it to work correctly?
P.S. is it possible to start 2 new menus at a time? for ex: I need 1. main menu 2. footer menu (any link to an article would be nice)
By default, Zend Framework's autoloader doesn't autoload controllers in the same way it loads other components (models, view helpers, forms, etc), so PHP throws the error saying it can't find the class. The quickest way to get around this is to explicitly include the controller in Bootstrap.php. The following should work:
public function _initMenus() {
require_once('./Controllers/IndexController.php');
$menuArray = new Menu_IndexController();
$outArray = $menuArray->menuGenerator(1);
$mainmenu = new Zend_Navigation($outArray);
$this->view->navigation($mainmenu);
}
It's pretty unusual to call a controller method during Bootstrap since there are many bootstrapping tasks upon which controller actions depend. In your case, the controller method menuGenerator() is not actually an action, so presumably it will not be a problem.
Nonetheless, it's still unusual enough that I would move the menuGenerator() method out into its own class. Then invoke that operation both at Bootstrap and in your controller.

add conditional logic to controller in zend framework

i want to add a conditional statement to my layout that tests:
the controller param in the url
the existence of a zend_auth()
what's the best way to achieve that? i have tried testing the $this->_getParam('controller') in the layout but got an error. i could just set that variable in all the controllers but that seems kind of dumb. how to best set a variable that i could use later from the layout with some conditional logic? or should i instead add my conditional logic that is inside a view helper and then loaded into the layout?
Edit The controller shouldn't be a URL parameter, unless you are doing some very strange routing. If you were getting a GET (or POST) variable, you would use ->getParam() on the request object, Zend_Controller_Front::getInstance()->getRequest(), as used below. But the controller is a separate property of that request object.
This is the auth part:
$loggedIn = Zend_Auth::getInstance()->hasIdentity();
This is the controller part:
$controller = Zend_Controller_Front::getInstance()->getRequest()->controller;

How do I avoid repetition when passing variables from the Controller/Action to the Layout

I am currently working on a project developed using Zend Framework, based on the structure of my web page design I have reached a point where I have to pass a small number of variables to my layout from each Controller/Action. These variables are:
<?php Zend_Layout::getMvcInstance()->assign('pageId', 'page1'); ?>
<?php Zend_Layout::getMvcInstance()->assign('headerType', '<header id="index">'); ?>
The reason for passing this information is firstly, I pass the page id as the multi column layout may change depending on the content being displayed, thus the page id within the body tag links the appropriate CSS to how the page should be displayed. Secondly I display a promotional jQuery slider only on the index page, but I need the flexibility to have it displayed on potentially multiple pages in case the wind changes and the client changes their mind.
My actual question: Is there a more appropriate method of passing this information to the Layout that I am overlooking?
I am not really questioning whether the information has to be sent, rather is there some Zend Framework feature that I have, in my haste, overlooked which would reduce the amount of repetitive redundant code which may very well be repeated in multiple Actions within the same controller?
You could turn that logic into an action helper than you can call from your controllers in a more direct way. You could also make a view helper to accomplish the same thing but view helpers usually generate data for the view rather than set properties.
// library/PageId.php
class Lib_PageId extends Zend_Controller_Action_Helper_Abstract
{
public function direct($title, $pageId, $headerType)
{
$view = $this->getActionController()->view;
$view->headTitle()->append($title);
$view->pageId = $pageId;
$view->headerType = $headerType;
}
}
In your controller actions you can now do this:
$this->_helper->PageId('Homepage', 'page1', 'index');
// now pageId and headerType are available in the view and
// Homepage has been appended to the title
You will also need to register the helper path in your Bootstrap like this:
protected function _initActionHelpers()
{
Zend_Controller_Action_HelperBroker::addPrefix('Lib');
}
Doing it like that can reduce the amount of repetitive code and remove needing to assign the values from the view. You can do it in the controller very quickly. You can also have default values in the case that the helper hasn't been called.
You shoudn't really be passing anything from the view to the layout, for a start the view should be included IN the layout, not the other way around.
So, setting your page title should be done using similar code to what you have, but inside the controller action being called:
$this->view->headTitle()->append('Homepage');
And the other two issues - you need to rethink as I stated to begin with. Maybe you're misunderstanding the layout/view principle? If you include the different views per action, then you simply change the div id when needed, and include the header for your banner only in the index.phtml file.

Line breaks in Zend Navigation Menu labels

I have a need to create a <br/> tag in the display label for a menu item generated using Zend_navigation, but don't seem to be able to find a way to do so.
My navigation item is defined in the XML config as:
<registermachine>
<label>Register your Slitter Rewinder</label>
<controller>service</controller>
<action>register</action>
<route>default</route>
</registermachine>
I want to force a tag in the output HTML between 'your' and 'slitter', such that it appears on two line as below:
Register your
Slitter Rewinder
However, I can't seem to do it. obviously using in the XML breaks parsing, and using html entities means that the lable is displayed as:
Register your <br/>Slitter Rewinder
Has anyone had experience of this that can offer advice?
Thanks in advance!
there is no such option built-in you have to use a partial
$partial = array('menu.phtml', 'default');
$this->navigation()->menu()->setPartial($partial);
echo $this->navigation()->menu()->render();
http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.navigation.menu
you may also try a hack with <label><![CDATA[Menu label<br/>Second line]]></label>
I found a (hacky) solution:
I updated my navigation.xml to use {br} tokens wherever a <br/> tag is required, and then amended the base Zend/View/Helper/Navigation/Menu.php file as follows:
within htmlify function, changed
$this->view->escape($label)
to
str_replace("{br}", "<br/>", $label)
I could (and probably will) override the Zend Library Menu View Helper with my own at some point, but this at least cover it for now.
there is a escapeLabels boolean used to convert html tags and it's true by default.
You can set your navigation like this
$this->navigation()
->menu()
->escapeLabels(false)
->...
http://framework.zend.com/apidoc/2.0/classes/Zend.View.Helper.Navigation.Menu.html#escapeLabels

Loading view file into a variable in Zend Framework

I am trying to send mail using mail templates. To do this I want to load a .tpl into a variable. Instead of loading an HTML file and substituting placeholders, I wonder if it is possible to set values of the view in the controller, and then load this view into a variable. This way I would have a variable containing the HTML mail filled out with the information set in the controller prior to loading the view.
Any alternatives are also welcome, I mean, if there are already ways of doing mail templating in a more standardized way.
A great idea Erik, and I've done this many times.
Zend_View is really just a templating system, and can be used to generate anything, not just HTML.
Sample code - create a view, assign some data, render the view and send the mail!
$view = $this->getHelper('ViewRenderer')->view;
$view->email = $data['email'];
$view->password = $data['password'];
$text = $view->render('mail/new-user.php');
$mail = new Zend_Mail();
$mail->addTo($data['email'], $data['forename'] . ' ' . $data['lastname']);
$mail->setSubject('Account Details');
$mail->setBodyText($text, 'utf-8');
$mail->send();
In the first line I retrieve the ViewRenderer's view so I have access to the normal script paths. You can create a new Zend_View object, but you'll need to add the path to your view scripts manually.
In my example text based content is generated, but you could generate HTML all the same.
Sorry guys (and girls). My google skills must have noticed it was early in the morning. I found what I was looking for here:
http://myzendframeworkexperience.blogspot.com/2008/09/sending-mails-with-templates.html