Hey :),
Glad you enjoyed the blog series. I had a ball writing it too. I'll just hit
one of your questions for now, will get to others when I have more time. When
the Wiki is back up, definitely check out the Proposal. I've made some changes
and did some refactoring since the earlier blog posts. (If you decide to use
the proposal code, you might want to subclass it so your previous API is
maintained for older templates).
On translating, you have a few options. The simplest is to make the translation
object itself a View parameter so you have:
<?php echo $this->translate->_("Welcome to the Blog!"); ?>
Or more accurately, taking security in-depth:
<?php echo $this->escape($this->translate->_("Welcome to the Blog!")); ?>
If that looks a little too convoluted, you could add a small View Helper which
just downsizes the interface a little for the Translation object.
<?php echo $this->escape($this->translate("Welcome to the Blog!")); ?>
I'd prefer the second - it's smaller, neater, and makes no assumptions about
the Translation object's API (since that is refactored to the View Helper)
making it easier to switch in a different library without the _() function
defined. If you want to go even further, you can make some assumptions about
translations, and let the, e.g. Zps_View_Helper_Translate helper automatically
escape for you unless specified otherwise:
<?php echo $this->translate("Welcome to the Blog!"); ?>
Here's a quick skeleton helper as an example (assumes you set the Translation
object as Zend_View::$translate).
class Zps_View_Helper_Translate
{
public $view = null;
public function setView(Zend_View_Interface $view)
{
if (!isset($view->translate) || !$view->translate instanceof
Zend_Translate) {
require_once 'Zend/View/Exception.php';
throw new Zend_View_Exception('Instance of Zend_Translate is not
available from this View', $view);
}
$this->view = $view;
}
public function translate($string, $escape = true)
{
if ($escape === false) {
return $this->view->translate->_($string);
}
return $this->view->escape($this->view->translate->_($string));
}
}
The last piece of the puzzle could be integrating sprintf() support so you can
replace placeholders in the Translation string with dynamic values. For
example, "Welcome, %1\$s!" (let's say it's "Willkommen, %1\$s" in Deutsche when
translated) to "Willkommen, Maugrim!".
<?php echo $this->translate("Welcome, %1\$s!", $this->user->name); ?>
Results in: "Willkommen, Maugrim!" (n.b. escaping always takes place last).
Hope this was of help.
Regards,
Paddy
Pádraic Brady
http://blog.astrumfutura.com
http://www.patternsforphp.com
----- Original Message ----
From: "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
To: Zend Framework General <[email protected]>
Sent: Wednesday, June 13, 2007 10:47:31 AM
Subject: [fw-general] Zend_View, Complex View
Hi,
I just start to work with ZF, I'm a beginner, I don't speak english very
well, please be indulgent.
I would like to thank Pádraic Brady about his different articles about
complex Views.
I use your Zend extension (ZPS) which uses "Composite Pattern", and "Factory
Pattern".
http://blog.astrumfutura.com/archives/285-Complex-Views-with-the-Zend-Framework-Part-4-The-View-Factory.html
I'll read your Zend_View Enhanced proposal with attention.
I've done some tests and I've some questions, some interrogations.
I wonder if what I've done can be considered as a good pratice, if I respect
all concepts.
I use a "main template" (layout) and sub-views. My "main template" receive
by the controller the name of views it must process, and send some data to
the views.
My "main template" looks like this :
---- Main template (sample : 'templates/main.php') ----
<!DOCTYPE html ....
<html>
<head>...</head>
<body>
<div id="global">
<div id="header"><?=$this->attach($this->header['tpl'], null,
$this->header['params']);?></div>
<div id="center">
<div id="content"><?=$this->attach($this->content['tpl'], null,
$this->content['params']);?></div>
</div>
<div id="footer"><?=$this->attach($this->footer['tpl'], null,
$this->footer['params']);?></div>
</div>
</body>
</html>
---- / Main template ----
Some parts of the template are defined by the init() fonction of the
controller (ex : $this->header, $this->footer).
The other parts depends of the asked action.
---- Controller (partial code) ----
public function init(){
parent::init();
...
$this->view->header = array('tpl' => 'templates/header.php',
'params' => null);
$this->view->footer = array('tpl' => 'templates/footer.php',
'params' => null);
...
}
public function indexAction(){
....
// Retreived data form Model => $clientResult
....
$this->view->content = array('tpl' => 'index/clients.php',
'params' => array('data'
=> $clientResult,
'translate' => $InstanceOfZend_Translate)
);
echo $this->view->render('templates/main.php');
}
---- / Controller ----
I need to translate some static sentences in the views. The controller send
an "instance of Zend_Translate" and some data to the views.
I wonder if it's a good way to work. Send a Instance of Zend_Translate to
the view, or create the instance in the views ?
---- View ('index/clients.php') ----
<h1><?=$this->translate->_('Identification');?></h1>
<ul>
<?php
foreach ($this->data as $key => $value){
echo '<li>'.$value['cli_name'].'</li>';
}
?>
</ul>
---- / View ----
Thanks for all returned comments.
Regards,
Fabien (FR)
___________________________________________________________________________________
You snooze, you lose. Get messages ASAP with AutoCheck
in the all-new Yahoo! Mail Beta.
http://advision.webevents.yahoo.com/mailbeta/newmail_html.html