Zend Framework: headTitle()->append() issue - zend-framework

Has anyone run into this problem...
In my layout.phtml I have:
<head>
<?= $this->headTitle('Control Application - ') ?>
</head>
then in index.phtml I have:
<? $this->headTitle()->append('Client List'); ?>
I expect that, when I go to my index action, the title should be 'Control Application - Client List' but instead I have 'Client ListControl Application - '
What is going on? How can I fix this?

Default behaviour of the headTitle() is to append to the stack. Before calling headTitle() in layout.phtml, your stack is:
Clientlist
Then, you call headTitle with the first argument and no second argument (which makes it default to APPEND), resulting in the following stack:
ClientListControl Application -
The solution, in layout.phtml:
<?php
$this->headTitle()->prepend('Control Application -');
echo $this->headTitle();
?>

Additionally, you can use the setPrefix method in your layout as such:
<head>
<?= $this->headTitle()->setPrefix('Control Application') ?>
</head>
And in your controllers/actions/etc use the standard append/prepend:
<?php
$this->headTitle()->setSeparator(' - ');
$this->headTitle()->append('Client List');
?>

I don't actually use headTitle, but do use ZF, and I had a quick look on the mailing list, this might solve the problem:
<head>
<?= $this->headTitle('Control Application') ?>
</head>
Then:
<?php
$this->headTitle()->setSeparator(' - ');
$this->headTitle()->prepend('Client List');
?>

This happens because the layout is the last script to be executed. So you actually do the append BEFORE the set of the title, so that there's nothing to append to yet.
Set the main title (Control Application) in a Controller. For example I always do it in the predispatch action of a initPlugin so that it is execute before any other Controller Action, and I can append or prepend at will.
To use such a plugin just define a new Class extending Zend_Controller_Plugin_Abstract and define a function preDispatch(Zend_Controller_Request_Abstract $request) where you can put all your common-to-the-whole-site code, and to register the plugin just put it into the controllerFront of your bootstrap: $controller->registerPlugin(new InitPlugin());

Related

MVC includes/partials (Codeigniter)

I've got an issue at the moment where I have two methods and two views. Each method passes it's Db data to a view, as expected with the MVC pattern.
My issue arises because I want one of the views to be a partial, which is included in the other view.
So it looks something like this:
First view - i.e. a form
Second view - fragment of form, i.e. list of cities
I want the second view to be it's own unique file, how can I include this in the first form, at any location in the file?
** had to edit as I got the first bit wrong. There is only one controller, with two methods passing data to two views.
I don't know if I understand you correctly, but CodeIgniter has the ability to return a view as data. In the Controller it would look like this:
$data = array();
$data['partial'] = $this->load->view('partial_view', $data, true); // the 3rd parameter let's the view return as data
$this->load->view('whole_view', $data);
The whole_view file would look something like this:
<html>
<head></head>
<body>
<h1>View 1</h1>
<?php echo $partial; ?>
</body>
</html>
The logic, that loads the data for the partial_view could be extracted to a model or so.
Hope this helps!
You can include the load like this:
view_one.php
<html>
<head></head>
<body>
<h1>View 1</h1>
<?php echo $this->load->view('view_two'); ?>
</body>
</html>

where to write jquery ajax in yii form

I"m trying to write custom auto complete for text field. can anyone pls tell me where to write this jquery, ajax code for this textfield in yii..
<div id="output" class="row">
<?php echo $form->labelEx($model,'id'); ?>
<?php echo $form->hiddenField($model,'id'); ?>
<?php echo $form->textField($model,'id');
'$(function () {
$("#search").change(function(){
$.ajax({url:BASE_URL + '/controller/lookup/',
type:"POST",
data:this.value,
success:function(data){
$("#output").html(data);
}
});
});
});'?>
any help pls,
Many Thanks
Try Yii::app()->clientScript->registerScript instead of Yii::app()->getClientScript()->registerScript
You should just make a new file, call it, 'myFuncs.js'. Place in a directory within your Yii Web App.
Then, in your view, simply call the js file.
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl.'/path/to/your/scripts/myFunc.js');

apply class active to anchor tag in Zend_Navigation

i am using partials to decorate Zend_Navigation to fetch desired output. everything is working good except i am having difficulty adding class="active" in <a href..> tag. here is my layout partial sidebar.phtml
<ul id="menu" class="nav">
<?php foreach($this->container as $page): ?>
<li class="<?php echo $page->class; ?>"><span><?php echo $page->label; ?></span></li>
<?php endforeach; ?>
</ul>
in the <a href="<?php echo $page->getHref(); ?>"> i want to add class="active" for the current page.
i tried some solutions which i found after searching. but nothing worked for me.
most of the solutions talk about doing it in controller for example
$page = $this->view->navigation()->findOneByLabel('Your Label');
if ( $page ) {
$page->setActive();
}
i haven't tested this yet. as i am using multiple navigations in the layout from one single navigation.xml file. i was wondering if is there a way i could do it in partials itself instead of in controllers or other helpers?
thank you
It's easy. In the view partial you can check which page is active:
if ($page->isActive()) { ... }
From ZF documentation:
Note: Note that when using the route property in a page, you should also specify the default params that the route defines (module, controller, action, etc.), otherwise the isActive() method will not be able to determine if the page is active. The reason for this is that there is currently no way to get the default params from a Zend_Controller_Router_Route_Interface object, nor to retrieve the current route from a Zend_Controller_Router_Interface object.

symfony 1.4 FORMS - how to replace default (related) dropdown field by code generated in module / component?

I've been creating with a ajax and doctrine chain-dropdown to select a category. I put them in component. It works like this:
http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php
Im trying to replace default category dropdown build on schema and related with current table. When in
/lib/form/doctrine/TabbleForm.class.php
I remove a field with code:
unset($this['category']);
And in the tamplate which has a form body I paste a component code (with this dynamic ajax-based dropdown):
include_component('add', 'selectcategory',array('catid' => 0));
I have notice:
Unexpected extra form field named "category".
How can I replace that default code with category-field code from component / module?
How do you display your form in the template? Using a basic <?php echo $form; ?> or manually like:
<?php echo $form['field']->renderLabel() ?>
<?php echo $form['field']->render() ?>
<?php echo $form['field']->renderError() ?>
I suggest you to use the second method, at least using <?php echo $form['field']->renderRow() ?>. This way, you won't have to unset the categoy field from your Form class. But you will have to name the select tag in your component the same way the form do.
If the form display:
<select id="formname_category" name="formname[category]">...</select>
You will have to use the same name (for the second select I guess). Then, you won't have any problem in your form and, more important, the validator related to your category field will also work!

Zend reusable widgets / plugins / miniapplications?

I'm new to Zend framework and trying to get some insights about code re-usability. I definitely know about modules but there seems to be a bit of uncertainty about what functionality should go into modules and what not.
What I'm trying to accomplish:
1) to have reusable mini programs/widgets/plugins (whatever you may call them) that one can simply plug into any site be doing this in layout or view:
<?php echo $this->contactform;?>
or this in the view:
<?php echo $this->layout()->blog;?>
I'd call them extension. so basically sort of what you'd see in Joomla/ WordPress/Concrete5 templates.
2) All code that is related to that specific extension should be in it's separate directory.
3) We should be able to output extensions only for certain modules/controllers where they are required. they shouldn't be rendered needlessly if it won't be displayed.
4) each extension may output multiple content areas on the page.
Do you have a nicely laid out structure / approach that you use?
It sounds like you need study up on view helpers. View helpers can be a simple as returning the App Version number or as complicated as adding html to multiple place holders. For example:
layout.phtml:
<h1><?php echo $this->placeholder('title'); ?>
<div class="sidebar">
<?php echo $this->placeholder('sidebar'); ?>
</div>
<div class="content">
<?php echo $this->layout()->content; ?>
</div>
in your view script foo.phtml for example:
<?php
$this->placeholder('title')->set('Hello World!');
$this->placeholder('sidebar')->set('Hello World!');
?>
<h1>Bar Bar!</h1>
Now if you want to be able to reuse that over and over again you can do this:
<?php
class Zend_View_Helper_MyHelper extends Zend_View_Helper_Abstract
{
public function myHelper()
{
$this->view->placeholder('title')->set('Hello World!');
$this->view->placeholder('sidebar')->set('Hello World!');
return '<h1>Bar Bar!</h1>';
}
}
Now, replace the code in your foo.pthml with:
<?php
echo $this->myHelper();
Both examples of foo.phtml output:
Hello World!
Hello World!
Bar Bar!
Of course this is very simplified example, but I hope this helps point you in the right direction. Happy Hacking!