How do i use translate in the form using Zend? - zend-framework

<?php echo $this->translate("Hello World"); ?>
This works inside view. How do I get the same in the form?

In a Form, you can try this :
$this->getView()->translate("Hello World");

Related

Getting HTML of a Cake PHP form

I am creating a form using Cake PHP. Is there any way to retrieve the basic HTML of the created form. For example,if we use Form Helper, we can create form using PHP itself. But now, I need only the html part of the created form for other use. Is it possible to retrieve it??
For example, say if I give input form like,
<?php
echo $this->Form->create('User');
echo $this->Form->input('email');
echo $this->Form->end('Save');
?>
I need output like this
<form action="index.html">
<input type="email" />
<input type="submit" value="Save" />
</form>
I can even create seperate function for attaining this objective. But I would like to know, if there is any other method for achieving this output
you can store FormHelper output in a string variable
<?php
$html_string = '';
$html_string .= $this->Form->create('User');
$html_string .= $this->Form->input('email');
$html_string .= $this->Form->end('Save');
?>
and then use your string elsewhere. But I'm not sure this is what you're searching for.
If I understand the question correctly, you want to use the form you created on another part of your site?
If that is the case, I would put the form itself in an Element and then call the Element wherever I wanted the form.
View/Elements/form.ctp
<?php
echo $this->Form->create('User');
echo $this->Form->input('email');
echo $this->Form->end('Save');
?>
Then in any view on your site you can call the Element using:
<?php echo $this->element('form');?>

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');

Format date in the indexSuccess.php

I want to format the created_at field date from the original for something like 03.May.2011 to be shown in the indexSuccess.php and in the showSuccess.php
Could you help me?
thanks
You can make in symfony in your indexSuccess.php and showSuccess.php instead of for example:
<?php $value->getCreatedAt() ?>
next:
<?php echo date('d.M.Y', strtotime($value->getCreatedAt())) ?>
You can use other formats.
The format of some data absolutely not belongs into controller context - so please use
use_helper("date");
echo format_date($myDate);
from symfony's date helper in your template (showSuccess.php, blaSuccess.php) or partial (form.php, list.php, test.php) !
You'll find more informations here http://www.symfony-project.org/gentle-introduction/1_4/en/13-I18n-and-L10n#chapter_13_sub_outputting_data_in_the_user_s_culture or in the source file.
I believe the date is returned in a string format Y-m-d H:i:s, as fits the MySQL datetime type. It would be nicest to convert this to a PHP DateTime instance using DateTime::createFromFormat, presuming you are using PHP > 5.3.
So, in your controller:
$this->creation_date = DateTime::createFromFormat('Y-m-d H:i:s', $item->created_at);
Then, in your view:
<?php echo $creation_date->format('d.F.Y') ?>
See also DateTime::format
If you are using PHP 5.2 or before, you can't use createFromFormat. You probably want to fall back on strtotime:
$this->creation_date = strtotime($this->created_at);
and in the view:
<?php echo date('d.F.Y', $creation_date) ?>
To create a datestring like your example (03.May.2011) use this way:
<?php use_helper("date"); ?>
<?php echo format_date($myDate,'dd. MMM. yyyy'); ?>

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!

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

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());