Dynamically include .js files from Zend_Controller_Action? - zend-framework

I came across the case where depending on the execution path I may need to invoke an inclusion of .js file from controller. Is there a nice way of doing it? (besides setting some view variable with actual .js include code)?

See the view helper headScript(). I'm just writing this off the top of my head but I think it works like this:
From within a view file: $this->headScript()->appendFile('filename.js');
From within a controller: $this->view->headScript()->appendFile('filename.js');
And then somewhere in your layout you need to echo out your headScript object:
<?=$this->headScript();?>

Sure, you could do like #Bill Karwin described.
But if you would like to do it really nicely, you need template inheritance - like it is implemented in Django framework for Python for instance. There are some extensions for Zend Framework as well, take a look at Calypso.

Related

How main.scala.html get called in Play Framework

I am trying to understand the architecture of Play Framework (internally how it works). In other framework like struts we can create index.html page and place in web folder with entry in web.xml file.
In Play the start point is main.scala.html page and all other pages inherited from it by placing all the content wrapped in
#main(title =""){
}
like index.scala.html page. But I couldn't find the place where main.scala.html page is registered to Play framework (no entry in routes file or other place?) or may be I am missing some points here.
So far from the play docs I understood that every scala.html page is basically a method call which sounds right to me.
So my goal is basically create other pages like main.scala.html page which will act as container for other sub pages and arrange my code in more modular way
Sorry if I am asking a dumb question.
Thanks in Advance
According to the Play docs main.scala.html is a layout - just common view which allows to inject HTML from other views and references to it by (content: Html) . main layout just wraps code that index view injected to it. (with optional arguments)
The opposite situation is usage of include or tag so just pointing the place where another view should be placed in the current view (also with optional arguments).
You don't need use them at all - Play doesn't enforce you to do that, you can use separate view (without layout) for each action.
On the other hand if some number of views should share same set of JS and/or CSS then layout(s) are native choice to do that job.
For an example instead of using main.scala.layout you can create ie. frontend.scala.html, backend.scala.html, guest.scala.html so your FE views will use #frontend(){ code...}, BE #backend(){ code... } etc.
You are unlimited in number of layouts/views/tags and other includes ;) All belongs to you. That's Play!

Zend Layout Placeholder?

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?

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'

Is it possible to directly edit the XML of XIB files?

I tried to do it, but Interface Builder refuses to open the resulting file:
alt text http://img81.imageshack.us/img81/7194/screenshot20091102at252.png
"Interface Builder was unable to determine the type of [file name]"
To answer your question (as you put it in the title): Yes, it is. I did it with TextWrangler for a test (moved a widget) and it works like a charm.
I suspect, you also changed something else (encoding, file name) when you were editing the file. You should try it again with a copy of the xib and do a diff between original and result.
It is possible. Just the task's complexity is approaching the complexity of writing an web app in assembler.
Joking. It's highly not recommended. I think this Xib is not your and Interface builder is just not allowing you to use it.
While the XML format of XIB files is intended to be somewhat human readable, XIB files should never be edited by hand. While the file may appear mergable, the object graph is complex and cannot be maintained except when edited within Interface Builder.
Even if it appears to work, this is unsupported and could lead to more complex problems down the road. It is better to instead file enhancement requests on Interface Builder to provide the capability you need, rather than try to hand edit the XML.

Template declaration to dynamically find views in RoR

I'm trying to separate the views for the different platforms into different subfolders.
I have done this for the layout, at the moment I have the following:
class MoviesController < ApplicationController
layout :site_layout
def site_layout
if(iphone_request?)
"iPhone/movies"
else
"movies"
end
This means that in my action methods I don't need to include :layout, however I do still need to manually include the path to the template.
format.iphone {render :template => 'movies/iPhone/index'}
Is there a way to have the same kind of layout declaration but for templates?
Thanks
Ben
You may want to extend the view_paths so that you can have a special iphone subfolder under views and override templates as necessary. See this tutorial on how to do that.
However, is there a reason you don't want to use the iphone format in the view name (show.iphone.erb) instead of making a subfolder? See martinkl's answer in your other question for details.
I might be off, but maybe it'll help - try checking prepend_view_path.