views:

138

answers:

3

I am using the MVC functionality in the Zend Framework 1.9, and it appears that Zend_Layout is not encoding the view content using UTF-8, despite this being set in the heading.

The layout script is shown below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
     <?php echo $this->headTitle(); ?>
     <?php echo $this->headMeta(); ?>
     <?php echo $this->headLink(); ?>
     <?php echo $this->headScript(); ?>
    </head>
    <body>
     <p>£ $ &pound;</p>
     <?php echo $this->layout()->content;  ?>
    </body>
</html>

The view script can be found below.

<p>£ $ &pound;</p>

In the layout, the pound symbol outputs as you would expect, but it doesn't render inside the actual view.

If I remove the layout and just output the view, it comes out fine. Also if I put the $this->layout()->content inside utf8_encode it all works dandy.

I just want to know if there is a way to fix this issue IN the Zend Framework.

A: 

Hi,

By default, Views are encoded in ISO-8859-1, and not UTF-8 ; maybe this is the reason for your problem.

You'll find more explanations and an example in this section of Survive the Deep End : 5.4. Step 3: Implement Application Bootstrapping -- it's a bit long, but some stuff there might help you...

Here is a quote from that page :

At present, Zend_View, in stark contrast to other components, uses a default character set of ISO-8859-1 (as do View Helpers) which simply won't do if you are going to output multibyte characters

And also :

To change the default, we should instantiate a new instance of Zend_View, set a more appropriate default character encoding of UTF-8, and implant this altered View object into the ViewRenderer Action Helper.

And there is another way, using the .ini configuration file, explained in the next chapter ; especially, section 6.5. Step 4: Handling Setting Of Standard Component Defaults.

Pascal MARTIN
The first thing I did was to use setEncoding on the view in order to change it to UTF-8 but this didn't seem to make any difference. Should I have done anything more than this?
Philip Bennison
+1  A: 

What is the file-encoding of your layout and your view file? Make sure that both of them are encoded with UTF-8. I strongly suspect that your view file (the one with <p>£ $ &pound;</p>) is not UTF-8-encoded.

The reason why you see the described behaviour is that the browser guesses the used encoding and switched to UTF-8 when using the layout and to some other encoding (most likely ISO-8859-x) when just displaying the view script.

You should provide the browser with a decent hint on which encoding is used. One option is using a meta http-equiv tag in your layout's <head>-section, another one is setting an HTTP-header (Content-type: text/html; charset=utf8) on your response object.

Stefan Gehrig
Thanks. Such a simple thing to overlook. I must have some ooooold PHP files that I keep cloning. Need to do some spring cleaning.
Philip Bennison
A: 

Do this in your bootstrap.

$view->setEncoding('UTF-8');
$view->doctype('XHTML1_STRICT');
$view->headMeta()->appendHttpEquiv('Content-Type',  'text/html;charset=utf-8');

I generally use this function in my bootstrap

protected function _initDoctype()
{
$this->bootstrap('view');
    $view = $this->getResource('view');

    $view->addHelperPath(.....);
    $view->addHelperPath(.....);

    $view->setEncoding('UTF-8');
    $view->doctype('XHTML1_STRICT');
    $view->headMeta()->appendHttpEquiv('Content-Type',  'text/html;charset=utf-8');

}

Alternatively:

In your layout file

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

And dont echo $this->headmeta() at all.

Krishna Kant Sharma
If you are bootstrapping view by $this->bootstrap('view')Then in your config.ini file add thisresources.view[] =
Krishna Kant Sharma