Zend Framework translate language text - zend-framework

I have some translation code which is working fine.
<?php echo $this->translate("54"); ?>
Outputs
Hello World
is it possible to output instead of above
<div class='lang' id='54'>Hello World</div>
Later using jquery I would like to manipulate the div.

create a custom view helper named MyTranslate extends \Zend\I18n\View\Helper\Translate then override the _invoke method :
public function __invoke($message, $textDomain = null, $locale = null)
{
$t = parent::__invoke($message, $textDomain , $locale);
//change the value of $t however you wnat
return $t;
}
if you don't what to change your code where ever you have used translate register this new view helper as translate and not my_translate

If you use Zend1, you can use a view helper like this:
Create a Helper directory in your library
for example, in my case I have this directory:
library/DoyDoy/Helper/
Create your helper like this:
library/DoyDoy/Helper/TranslateID.php
<?php
class DoyDoy_Helper_TranslateID extends Zend_View_Helper_Abstract
{
public function translateID($id)
{
return '<div class=\'lang\' id=\'' . $id . '\'>'. $this->view->translate($id) . '</div>';
}
}
Add your Helper in the bootstrap:
protected function _initDoyDoyView(){
$this->bootstrap('view');
$view = $this->getResource('view');
$view->addHelperPath('DoyDoy/Helper/', 'DoyDoy_Helper');
}
In your view, call the helper like this:
<?php echo $this->translateID("54");?>
This should display:
<div class='lang' id='54'>Hello World</div>
I hope it will help you :)

Related

Zend pass variable from plugin to view

In plugin I try $this->view->menu = $menu; , but in view I try <?php var_dump($this->menu); ?> and get NULL
Maybe are solution to pass variable from plugin to view ?
you can use like below
First : Without using helpers or plugins do :
Zend_Layout::getMvcInstance()->assign('whatever', 'foo');
After this you can use the following in your layout:
<?php echo $this->layout()->whatever; ?>
This will print "foo".
OR
Second :
<?php
class My_Layout_Plugin extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$layout = Zend_Layout::getMvcInstance();
$view = $layout->getView();
$view->whatever = 'foo';
}
}
then register this plugin with the front controller, e.g.
Zend_Controller_Front::getInstance()->registerPlugin(new My_Layout_Plugin());

How to include js and css in Zend Form Elements?

I create my own zend form element - App_Form_Element_Wysiwyg
<?php
class App_Form_Element_Wysiwyg extends Zend_Form_Element_Xhtml
{
/**
* Default form view helper to use for rendering
* #var string
*/
public $helper = 'wysiwyg';
}
Then I create helper for wysiwyg - App_View_Helper_Wysiwyg
<?php
class App_View_Helper_Wysiwyg extends Zend_View_Helper_FormTextarea
{
public function wysiwyg($name, $value = null, $attribs = null)
{
// Here I want to include js and css for wysiwyg editor
...
$xhtml = '<textarea name="' . $this->view->escape($name) . '"'
. ' id="' . $this->view->escape($id) . '"'
. $disabled
. $this->_htmlAttribs($attribs) . '>'
. $this->view->escape($value) . '</textarea>';
return $xhtml;
}
}
How can I include js and css files for wysiwyg editor?
The headScript() helper lets you add or manipulate script tags in the header. So within your helper you can do:
$this->view->headScript()->appendFile('/path/to/file.js');
then just ensure your layout is calling the helper somewhere in the head section:
<?=$this->headScript()?>
to output the HTML. There is also the headStyle() helper which works in a similar way and can be used for your CSS.
Relevant docs:
HeadScript
HeadStyle

Error in view file

I'm new to zend framework.I'm getting the following error when trying to view the page
Fatal error: Using $this when not in object context in D:\xampp\htdocs\neemjobs\application\views\scripts\register\index.phtml on line 1
class RegisterController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$this->view->pageTitle = "Zend_Form Example";
$this->view->bodyCopy = "<p >Please fill out this form.</p>";
$form = new forms_ContactForm();
$this->view->form = $form;
}
}
My View is
<?php echo $this->pageTitle ;?>
<?php echo $this->bodyCopy ;?>
This is totally correct (on my Zend Framework installation goes perfectly, I just don't understand "in which way" are you using the Form... but, anyway, it works, so the "bug" is not there...

How to render a view with specific text?

I have a class like this:
class myData {
function render(){
$str = 'This is string.';
// have to code here
}
}
and a myview.phtml file:
<div id='someid'></div>
Q: Now I want to do something like this in another phtml file:
<?php
$obj = new myData ();
echo $obj->render(); // it should be <div id='someid'>This is string.</div>
?>
So how can I change my render function in myData class that it should get myview.phtml and place string between DIV tag(<div id='someid'></div>) and print.
Thanks
One possible solution could be to use Partial view helper. This helper can be used to " render a specified template within its own variable scope".
Specifically in myview.phtml you can add the following:
<div id='someid'><?php echo $this->myText; ?></div>
Then, in the another phtml you could have:
<?php
$obj = new myData ();
echo $this->partial('path/to/myview.phtml',array('myText' => $obj->render()));
?>
Maybe you are looking for something like this:
http://webgen.hu/class.html.txt
http://webgen.hu/class.html.php

Embedding persistent login form Zend

I've seen this question asked already - but none of the answers really gelled for me, so I'm asking again: I want to embed a persistent login form (which will change into a nav bar if logged in) in the header bar for a site. Effectively, I want to be able to inject some controller logic into the layout.
After much research, I can see several ways that might achieve this - none of which seem ideally suited.
View Helpers seem suited to adding a suite of methods to the Zend_View object - but I don't want to write conditional code in the layout.phtml to trigger a method. Action helpers would help me remove that functionality and call it from a Controller - but that seems to be in poor favour from several quarters. Then there are plugins, which might be well suited in the dispatch/authentication loop.
So, I was hoping someone might be able to offer me some guidance on which way might best suit my requirements. Any help is greatly appreciated.
For those of you with a similair issue, this is how I ended up solving it (I'm using layout btw)
I registered a view helper in the Bootstrap:
protected function _initHelpers(){
//has to come after view resource has been created
$view = $this->getResource('view');
// prefix refers to the folder name and the prefix for the class
$view->addHelperPath(APPLICATION_PATH.'/views/helpers/PREFIX','PREFIX');
return $view;
}
Here's the view helper code - the actual authentication logic is tucked away in model code. It's a bit clumsy, but it works
class SB_UserLoginPanel extends Zend_View_Helper_Abstract {
public function __construct() {
$this->user = new SB_Entity_Users();
$this->userAccount = new SB_Model_UserAccount();
$this->request = Zend_Controller_Front::getInstance()->getRequest();
$this->form = $this->makeLoginForm();
$this->message='';
}
//check login
public function userLoginPanel() {
if(isset($_POST['loginpanel']['login'])) {
$this->processLogin();
}
if(isset($_POST['loginpanel']['logout'])) {
$this->processLogout();
}
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$this->loginPanel = $this->getUserNav();
} else {
$this->loginPanel = $this->getLoginForm();
$this->loginPanel .= $this->getMessages();
}
return $this->loginPanel;
}
private function processLogin() {
if($this->form->isValid($_POST)){
$logindata = $this->request->getPost('loginpanel');
if($this->user->login($logindata['email'],$logindata['password'])) {
Zend_Session::rememberMe();
$redirect = new Zend_Controller_Action_Helper_Redirector();
$redirect->goToUrl('/account/');
return $this->getUserNav();
}else {
$this->message = '<p id="account_error">Account not authorised</p>';
}
}else {
$this->form->getMessages();
}
}
private function processLogout() {
if(isset($_POST['loginpanel']['logout'])) {
$this->user->logout();
$request_data = Zend_Controller_Front::getInstance()->getRequest()->getParams();
if($request_data['controller']=='notallowed') {
$redirect = new Zend_Controller_Action_Helper_Redirector();
$redirect->goToUrl('/');
}
}
}
private function makeLoginForm() {
}
private function getLoginForm(){
return $this->form;
}
private function getMessages(){
return $this->message;
}
private function getUserNav(){
//return partial/render
}
}
I then call this from the relevant part of the markup in the layout.phtml file.
<?php echo $this->doctype(); ?>
<head>
<?php
echo $this->headLink() ."\n";
echo $this->headScript() ."\n";
echo $this->headMeta() ."\n";
?>
<title><?php echo $this->escape($this->title) ."\n"; ?></title>
</head>
<div id="masthead">
<div id="userLoginPanel">
<?php echo $this->userLoginPanel(); ?>
</div>
</div>
<!--rest of layout-->
In principle, this should be an action helper, but after reading some less than favourable articles regarding Zend Action Helper - I opted for this method which did the trick.
Hope that helps!