dojo.require required in Zend Framework's view helpers? - zend-framework

I've tried using a Dojo button view helper, but it appears that Zend will not automatically generate a dojo.require('dijit.form.Button'). Is this correct behavior?
Here's excerpt from my layout script:
<head>
<?= $this->dojo()->setDjConfigOption('usePlainJson',true)->addStylesheetModule('dijit.themes.claro')->setLocalPath("/js/dojo/dojo.js"); ?>
</head>
...
<?= $this->button('test', 'test') ?>
And this is in my bootstrap:
public function _initDojo() {
$view = $this->getResource('view');
$view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
Zend_Dojo::enableView($view);
Zend_Dojo_View_Helper_Dojo::setUseDeclarative();
}
All other Dojo-specific code is rendered properly, except the dojo.require.

I've not used the dijit helpers on their own so I only have a partial answer for you.
ZF definitely automatically adds the appropriate requires when you use dijit elements in Zend_Dojo_Form. From looking at the helper code it looks like this should happen when you use them on their own as well.
If you are really calling $this->button in your layout as in your example, then I would guess that it's not working simply because the dojo helper has already run by the time your helper is called. You could try moving the same call to a view script instead to see if this solves the problem (view scripts are rendered before layouts).

Related

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 Framework: How to add custom html to Form Element?

I want to add Custom HTML ("Login" link near the "Register" button) in Register form with ZF 1.12.
When I do that with:
$submit = new Zend_Form_Element_Submit('register');
$submit->setDescription(" or <a href='auth/login'>Login</a>");
... then the link is placed in the next row, but I need it close to the "Register" button.
How can I implement it as simply as possible?
You have to create a custom form decorator for that. It is not that simple. If you have no other option then its better to create new decorator and add it to the form element. The ZF manual have a good documented section for that Creating Custom Form Markup Using Zend_Form_Decorator. This SO post will help too Insert custom HTML into Zend_Form.
there is also a quick workaround using javascript (if you are using jquery)
$(document).ready(function() {
$('#register').after(' or Login');
});
The description for the submit button, should surely be something more like:
'Activate button, to register.'
Arguably the login link isn't directly related to the form either. So I'd just place that text after the form.
<?php echo $form; ?>
<p>Or <a href='auth/login'>Login</a></p>
If you think the login link should always belong to the form, you could perhaps add it to the form description.

Zend navigation object empty in view phtml

I have a module based application with a default and a user module.
My Navigation menu which is called from the layout is working fine, but now want to create a sidemenu. I would like to call it from the e.g. language.phtml in user/settings/language.
in my language.phtml
...
<?php echo $this->navigation()->menu()
->setPartial('partials/sidemenu.phtml')
->render(); ?>
...
This partial is in the scripts directory in the user module.
The partial is being called but for some reason the navigation/container object is empty.
Thanks in advance!
Peter

how to append script file in 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.

Zend Framework - how to apply my own js plugin?

How can i add my own jQuery plug-in located in my zf path "public/js/isround.js"?
- to apply using Zend framework instead of manually putting this:
<script> $("#world").isRound('myPlugin'); </script>
jQuery setup is working
$this->jQuery()->setLocalPath('http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js')
->enable()
->setUiLocalPath('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js')
->uiEnable()
->addStylesheet('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/ui-lightness/jquery-ui.css');
file application/views/scripts/index/index.phtml, i have:
< div id="world"> _____my js plugin apply here _____< /div>
is this what you are looking for?
$this->headScript()->appendFile('/js/isround.js');
Use a view helper.
zend documentation
Check out:
Example #2 Building your own Helper with No Conflict Mode
Also a tutorial about viewhelpers and jquery Zendcast
Here is some code:
Create a folder in your library folder called Mylib.
In it create a folder views.
In views create a folder helpers.
In helpers create a file named: IsRound.php
<?php
class Mylib_Views_Helpers_IsRound {
public function isRound($elem){
echo '<script type="text/javascript">$("'.$elem.'").isRound();</script>';
}
}
In the indexAction in IndexController.php
$this->view->addHelperPath('Mylib/views/helpers', 'Mylib_Views_Helpers');
In index.phtml:
<?php $this->isRound('#elem'); ?>
Hope this helps!