how to append script file in zend framework - zend-framework

My layouts are placed inside layout/scripts/layout.phtml
i have placed the below code inside my head section of layout.phtml
<?php
print $this->headScript()->appendFile($this->baseUrl().'/js/jquery-1.7.2.min.js')
->appendFile($this->baseUrl().'/js/simpla.jquery.configuration.js');
?>
Now I want to append another javascript file from a view. For that I wrote the following code:
$this->headScript()->appendFile($this->baseUrl().'js/fancybox/jquery.fancybox-1.3.4.pack.js');
Although this appended the file but it appears before my jquery-1.7.2.min.js. What i want is that i want to add jquery.fancybox-1.3.4.pack.js below my jquery-1.7.2.min.js
How can i do this?

Your view script is rendered before the layout so the calls to appendFile() in your layout result in those scripts (jquery-1.7.2 and simpla.jquery) being appended after the one you appended in the view script.
To fix this, use prependFile() in your layout at least for the main jQuery script.
Your layout might look like this:
<?php
print $this->headScript()
->appendFile($this->baseUrl().'/js/simpla.jquery.configuration.js')
->prependFile($this->baseUrl().'/js/jquery-1.7.2.min.js');
No need to change the view script, that is fine as is.
See HeadScript Helper Example #23 which talks a bit about ordering of the scripts.
The important thing to remember that they don't mention is that your view script gets rendered before the layout does.

Related

Joomla 'PageNavigation' Plugin

If this isn't possible please let me know!
I'm hoping there's a solution to what I'm asking.
I need to move the Next/Prev buttons, located in the pagenavigation plugin to after the <jdoc:include type="component" />; Basically render it anywhere in my templates' index.php?
Is there any way to do this?
This is the code that renders the pagination:
<?php
if (!empty($this->item->pagination) && $this->item->pagination && $this->item->paginationposition && !$this->item->paginationrelative):
echo $this->item->pagination;
?>
<?php endif; ?>
As you can see the pagination is part of the item. As you can see if you look at the pagenavigation content plugin the pagination values are created in response to the onContentBeforeDisplay event. The plugin is hard coded to only work for articles in the single article view.
So to use it in a different component you would really need to create a second plugin for that component (or you could do any component or anything besides the single article view, that all would be easy to code using context).
To locate it in a different place in the single article view you would have to move the block of code to the desired location in the layout. Potentially you could also use css to locate the rendering of the block somewhere else on the page. (But more on this at the bottom.)
Unfortunately (but nor surprisingly given its name) onContentBeforeDisplay comes really late, in the view (unlike with pagination in the backend).
I always find it confusing because this frontend "pagination" property controlled by this plugin has nothing to do with backend pagination which is controlled by a JPagination object. I believe if is because of backward compatibility all the way to 1.0. ALso because the template chrome for pagination chrome are called pagination.php.
That leads me to the next thing I'll mention. You can make a file pagination.php and put it in the html folder of your template. You can see an example of this in the core template protostar. THat's where you would do the CSS or whatever other tricks you want to do to make the pagination do what you want. I think if you work hard enough at it (possibly using javascript or possibly calling that file from a module) you can pretty much achieve whatever you want.

Zend Framework: Saving complete page to a file

I need to add the main navigation/header from my Zend based site onto the top of a third party product. The third party product will allow me to include any file(s) on the server into their layouts. My thought was I would run a cron that would save the header part of my Zend layout to a hard coded html file each night. Then just read in the appropriate file on the third part.
So I tried:
$htmlcontent = $this->view->render('file.phtml');
then saving $htmlcontent to a file. It saves out everything from file.phtml correctly but excludes the layout/header, which the part I really need. How would I go about saving everything generated ( including layout) to a file.
thanks
summer
The view script is just a part of the whole layout. In a normal Zend Framework setup, you will have one layout in which your view scripts (eg, file.phtml) in your case. The layout file will look like this:
<html>
<body>
<div>My header here</div>
<?php echo $this->layout()->content; ?>
<div>I have a footer here.</div>
</body>
</html>
$this->layout()->content will hold your view script, according to the page you are in (eg, file.phtml when your call fileAction()).
So, to access the full HTML of your page, you have two options:
Use the good old file_get_contents and get the complete rendered HTML to a string with http request.
$htmlcontent = file_get_contents('http://yourdomain/index/file');
Get the layout instance of the Zend Framework, assign content to it, and render it to a string as follows:
$layout = $this->_helper->layout->getLayoutInstance();
$layout->content = $this->view->render('file.phtml');
$htmlcontent = $layout->render();

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 View Helper - Is it specific view related?

When we create a view helper, on a Zend application, will that helper be available for ALL the views or, should we somehow tell that THAT view helper is available to a specific view?
What if, on the view folder "something", we have more then one file ? Any of those files can call it?
Thanks a lot,
MEM
When you call a view helper, the framework will look within the paths defined via $view->addHelperPath(). Typically, such a call will include a pseudo-namespace as well as a path:
$view->addHelperPath('My/View/Helper', 'My_View_Helper_');
Then when you call a view helper in a layout or a view script:
<?php echo $this->someHelper() ?>
The framework will do a LIFO search, appending the prefixes (in the above case: 'My_View_Helper_') to the classname 'SomeHelper' and then attempting to load the file defined by the addHelperPath() mapping.
In the default setup, the framework pre-loads the Zend view helpers by calling:
$view->addHelperPath('Zend/View/Helper', 'Zend_View_Helper_');
which is why you can use all the Zend-provided view helpers right out of the box.
Since all this processing is independent of which view script is making the call, it will work in any view script. [There are actually some issues associated to calling view helpers defined in other modules, but that's a separate issue.]

Dynamically include .js files from Zend_Controller_Action?

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.