Zend Layout Placeholder? - zend-framework

I am trying to create a sidebar in my layout that has the behavior of a placeholder. I want to be able to define the contents of this placeholder once per controller. So every controller can add custom content to the sidebar but without the need to define it in any view.
I am kind of confused on how to go about that with Zend_Layout. Any help?

I have tried something similar. Here is what you can do.
Place this type of code in the layout.phtml script file. Somewhere near the top. You don't have to but this way you 'know' what placeholders you're using. Doing this in the layout is also a good idea because you can wrap html divs are whatever here and not worry about it in the views. The views can just worry about the content. After this, you can add content to the placeholders from the controllers and the views.
$this->placeholder('blah');
$this->placeholder('sidebar');
$this->placeholder('blunk');
If you don't want to create them in your layout, then you can do it in the controller like so,
$this->view->placeholder( 'sidebar');
.
Now, you can either put content into it in the controller, or in the view script. Its a better idea to add the content in the view though.
In the layout you can then just echo the placeholders like so
echo $this->placeholder->( 'sidebar' );
All the views are executed BEFORE the layout is executed so any placeholders created by the views will be available to the layout to print out.
Also, controllers don't HAVE placeholders. Only views, and by extension layout, have placeholders like this so you have to declare them somewhere. Even if you declare them in the controller they still 'belong' to the view object.
I don't know if this helps at all but good luck. Tell me what you think.

How about adding a postDispatch() call to each controller?
public function postDispatch()
{
// code to populate/activate your placeholder
$this->view->placeholder('xxx');
}
This function will be called after your action completes. For more info, see Pre- and Post-Dispatch Hooks.

i have just implemented a solution to this that should work for most uses.
I store all of my placeholder.phtml files in the following dir:
/views/scripts/_placeholder
Within the placeholder i create directories for each Controller / Action that has a placeholder (as well as ROOT stuff). I then create a file for each placeholder.
e.g. Placeholder = sidebar. Controller = user / action = view
for the above we would store a file here:
views/scripts/_placeholder/user/view/sidebar.phtml
note: within the sidebar.phtml you will need to add : $this->placeholder("sidebar")->captureStart() and captureEnd();
if the plugin sees this file it will render it. If it doesnt find one then it wont.
Additionally the plugin will also look for the following and pull that in first:
views/scripts/_placeholder/sidebar.phtml
I can post the plugin if you want.
The only issue i have is i would like to now know if a placeholder has any data in it. That way i can create some layouts that are clever and will render what needs. DOes anyone know how to do this?

Related

Create / Edit / View the same form with Angular

I am creating a slightly elaborated form with Angular. This form can be submitted, then modified or simply displayed (with everything in read-only for example).
For now I have 3 templates with 3 controllers for each action (submit / edit / view) and the form is added as a partial (ng-include). The form has also its own controller. Is it the right way?
Also, should I make the form's controller the children of the templates' controllers or the opposite? I am using the same model for each action behind the form and I guess it should be injected through the template's controller.
It's my very first attempt to do this and I would like to have a few advice's and hints since I am afraid of going the wrong way. Thank you!
yo can use different template for each of them with single controller, different controller for each of them is not a good idea because there may be some common function in them then you need to write that function in each of them.
you can use common model for them,i am doing the same.
I see this is promising solutions to what you are looking for
can you try this http://vitalets.github.io/angular-xeditable/#editable-form

Change variable in Layout from view script

in my layout i have things like meta tags etc. that may change in e.g. in different static sites.
Example:
if i go to
/static/about
i would like to have a different meta-description as in
/static/contact
My Problem is that i would like to change these variables/values inside the view script static/content/about or static/content/contact and NOT in the controller.
How can i do that?
Take a look at the view helper: PlaceHolders
Take a look at this post may it useful for u :
http://blog.ggshow.com/index.php/reference/zend-framework-view-helper-placeholder

zend framework routing and layout

I have a CMS base controller that most of my other controllers extend. It has a default action for list, create, read, update and delete actions, all of them set the title based on the name of the resource(s) the user is working with.
The index action defaults to contain only one row:
$this->_forward('list');
Now my problem is that my previously set title is gone when I open the index of a CMS controller. I'd like to know what could be happening and what is the best solution of this.
Note that the problem does not appear if I rename my list action to index. It may also be relevant that changing the view title in the index action does not work.
Hmm, where do you set your title and in what way do you do it? Personally my titles are only two parts like "Application: current action". To achieve this i do the following:
//layout.phtml
<?=$this->headTitle()->setPrefix('APP: ')?>
//indexAction()
$this->_forward('list');
//listAction()
$this->view->headTitle()->append('List Data');
I do this for every action separately and since i do not assign a title within indexAction() i get the full wanted title "APP: List Data" inside listAction(). This remains true if i have my edit action like the following
//editAction()
if (!is_numeric($this->_getParam('id'))) {
return $this->_forward('list');
}
If this doesn't do it for you, your scripts will be needed to look for an error elsewhere :)
Actually, my initial idea of this issue having to do something with routing was right. I'm loading the layout in a preDispatch action of an action controller. So what actually happend was that I re-created my layout view after setting the title attribute in the index action.

Zend Framework Navigation - dynamically adding parameters

I am experimenting with Zend_Navigation to build breadcrumb for a web site. I created an XML file which lists the hierarchy of pages. Things are working fine for the most part except for pages that have dynamic parameters.
For example, there is a group page which has the URL " www.../groups/gid/1001". The id 1001 is dynamic so it changes for different groups. Because of that I cannot put it in the XML file. In that case, ZF generates a link without including any parameters, which of course won't work.
One solution I found is dynamically injecting the parameters to the Zend_Navigation object. This is working fine except that I need to do it for each action or controller.
Is there a better way to handle it? Does ZF have any classes to do this work?
I will appreciate any feedback.
Thanks!
http://zend-framework-community.634137.n4.nabble.com/Creating-a-dynamic-database-based-Zend-Navigation-item-td660805.html
And/or start reading here: http://framework.zend.com/manual/en/zend.navigation.introduction.html
At which point do you know the group ID to use in the navigation?
If it's early enough, you could simply create your own bootstrap init method to insert the navigation item, just remember to call $this->bootstrap('navigation') at the top of your method, eg
protected function _initCustomNav()
{
$this->bootstrap('navigation');
$navigation = $this->getResource('navigation');
// add custom item
return $navigation;
}
Disclaimer: I'm pretty sure the navigation resource is just called 'navigation'

Removing master layout from view (MVC2)

if i need remove master layout from my view, how can i do it in MVC2?
i tried put code in my view that was shown in documentation http://sparkviewengine.com/documentation/master-layouts: , but it still bring my Application.spark layout :-/
any ideas why?
I know this answer is a litle (maybe a lot) late but you can also use the PartialView method if you mean to render an HTML fragment instead of the full page.
Relevant Spark documentation
An Application.spark file in the Views/Layouts folder or Views/Shared folder
This is the most general-purpose way to have a site-wide master template. It will not be used if the controller returns a PartialView().
Not tested, but what if you create and empty master layout, and say <use master="EmptyMaster" /> in the top of your view? Or you could call on the empty master from the controller; return View("View", "EmptyMaster");
Don't know if it'll work, but it's worth a shot.