Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

problem with html_doctype plugin

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins
View previous topic :: View next topic  
Author Message
k-dub
Smarty Rookie


Joined: 30 Sep 2004
Posts: 9
Location: Land of Opportunity, State of Confusion

PostPosted: Thu Sep 30, 2004 3:42 pm    Post subject: problem with html_doctype plugin Reply with quote

I don't know if this is the right place for this, but I'm having trouble with the html_doctype plugin. I'm getting the
Code:
<?xml ....
declaration on line 2 - there's a blank line before the XML declaration. This is invalid for XHTML, but I don't see anything obvious in the plugin code to create this blank line.

Anyone have any thoughts / suggestions?
_________________
rm -rf /bin/laden
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Thu Sep 30, 2004 6:14 pm    Post subject: Re: problem with html_doctype plugin Reply with quote

k-dub wrote:
I don't know if this is the right place for this, but I'm having trouble with the html_doctype plugin. I'm getting the
Code:
<?xml ....
declaration on line 2 - there's a blank line before the XML declaration. This is invalid for XHTML, but I don't see anything obvious in the plugin code to create this blank line.

Anyone have any thoughts / suggestions?


your code?
Back to top
View user's profile Send private message
k-dub
Smarty Rookie


Joined: 30 Sep 2004
Posts: 9
Location: Land of Opportunity, State of Confusion

PostPosted: Thu Sep 30, 2004 7:03 pm    Post subject: Reply with quote

kills wrote:
your code?


Of course. Duh!

Here's index.php:
Code:
<?php

$page = isset( $_GET["page"] ) ? $_GET["page"] : "home";
$media = isset( $_GET["media"] ) ? $_GET["media"] : "all";

define( "SMARTY_DIR", "C:\\php\\Smarty\\libs\\" );
require_once( SMARTY_DIR . "Smarty.class.php" );
$smarty = new Smarty;
$smarty->caching = false;
$smarty->debugging = true;

$smarty->assign( "cssMedia", $media );
$smarty->display( $page . ".tpl" );
?>


I have home.tpl starting like this:
Code:
{html_doctype force_opera_html=true}
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">


I've tried adding "version=1.1" to the plugin declaration, as well as "type=Strict", but that didn't help. The only way the browser (Firefox 1.0 PR) won't error out is to use "sendheaders=false", which sends the content type as text/html. I also tried "xhtml=false", which the browser was OK with, but I'm going for XHTML.

FWIW, this is PHP 5.0.2 on Apache 2.0.52 on Windows XP Pro SP2. I also tried setting these two files with UNIX line endings (\n only) in case there was a problem with the Windows line ending (\r\n) - no difference.
_________________
rm -rf /bin/laden
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
k-dub
Smarty Rookie


Joined: 30 Sep 2004
Posts: 9
Location: Land of Opportunity, State of Confusion

PostPosted: Thu Sep 30, 2004 8:03 pm    Post subject: Reply with quote

I've also tried running it from the command-line:
Code:
php index.php
to see if it was an Apache2/mod_php problem. I got the same blank line before the XML declaration.
_________________
rm -rf /bin/laden
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
sage
Smarty Rookie


Joined: 09 May 2004
Posts: 20
Location: London, UK

PostPosted: Thu Sep 30, 2004 8:16 pm    Post subject: Reply with quote

Quote:

The only way the browser (Firefox 1.0 PR) won't error out is to use "sendheaders=false", which sends the content type as text/html.


setting sendheaders=false, will mean that the doctype header plugin does not send a mime type through the php header function, and the default mime type that your server uses will be sent (by apache or whatever).

What is the error that Firefox gives?

Unlikely, but do you have a blank line before the {html_doctype force_opera_html=true}, as it has to be on the fisrt line?
Back to top
View user's profile Send private message
k-dub
Smarty Rookie


Joined: 30 Sep 2004
Posts: 9
Location: Land of Opportunity, State of Confusion

PostPosted: Thu Sep 30, 2004 8:24 pm    Post subject: Reply with quote

sage wrote:
setting sendheaders=false, will mean that the doctype header plugin does not send a mime type through the php header function, and the default mime type that your server uses will be sent (by apache or whatever).


Yes, without this, Firefox does not attempt XML validation (I believe).

The Firefox error is:
Code:
XML Parsing Error: xml processing instruction not at start of external entity
Location: http://localhost:8080/smarty-test/
Line Number 2, Column 1:

<?xml version="1.0" encoding="UTF-8"?>
^


I've double-checked that the html_doctype plugin code is the first line in the template file. I'm sure I've done something wrong, as this is my first real attempt at PHP, but I can't find it anywhere.
_________________
rm -rf /bin/laden
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
sage
Smarty Rookie


Joined: 09 May 2004
Posts: 20
Location: London, UK

PostPosted: Thu Sep 30, 2004 9:17 pm    Post subject: Reply with quote

hmm... Thats an XML error I have not seen before Embarassed

Im using the doctype plugin with XHTML 1.1, and your code looks ok as far as I can see, when compairing against my code.

can you paste the plugin in its entirity as you have it?

(unfortunatly the last time I looked at it was in april, when I added the opera overide Sad )
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Sep 30, 2004 9:24 pm    Post subject: Reply with quote

try:
Code:
{html_doctype force_opera_html=true}<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
Back to top
View user's profile Send private message Send e-mail Visit poster's website
k-dub
Smarty Rookie


Joined: 30 Sep 2004
Posts: 9
Location: Land of Opportunity, State of Confusion

PostPosted: Fri Oct 01, 2004 2:49 am    Post subject: Reply with quote

Code:
<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     function
 * Name:     html_doctype
 * Purpose:  Adds a (X)HTML doctype to the current document.
 *           This plugin function needs to be called at the
 *           top of your template in the very first line.
 *           Default doctype is XHTML 1.0 Transitional.
 * Params:   xhtml             FALSE to use HTML doctype,
 *                             TRUE to use XHTML doctype
 *           version           HTML/XHTML version. For HTML 4.01, 4.0, 3.2 and 2.0 is supported.
 *                             For XHTML versions 1.0 and 1.1 are accepted.
 *           type              frameset, transitional, strict etc.
 *           dtd               TRUE to use default DTD, FALSE to not use a DTD or
 *                             path to DTD (e.g. "http://www.w3.org/TR/html4/Frameset.dtd")
 *           omitxml           if FALSE <?xml version="1.0" ... ?> header will be outputted
 *                             (only suitable if xhtml is set to TRUE)
 *           encoding          default is UTF-8
 *                             (only suitable if xhtml is set to TRUE)
 *           sendheaders       TRUE (default) or FALSE
 *           force_opera_html  Forces an html header and doctype if opera is detected
 *                             (only suitable if xhtml is set to TRUE). This is because the
 *                             script tag is not recognised by opera 7.23 and below in XHTML.
 *                             default is false.
 *                             
 *
 * Changes:  Version 1.5 (S. Shah)
 *           - Implemented a switch to force an html header and doctype when using
 *             opera (force_opera_html). This will give a html 4.01 doctype, allowing scripts
 *             to work.
 *           - Default type is now XHTML 1.0 Transitional
 *
 *           Version 1.4
 *           - Implemented better HTTP_ACCEPT header checks
 *           - Distinguish between HTML 4.0 and 4.01
 *           - minor fixes / clean ups
 *
 *           Version 1.3 (S. Shah)
 *           - Added support for XHTML1.1
 *
 *           Version 1.2b
 *           - fixed bug with incorrect xml doctype
 *
 *           Version 1.2
 *           - Now html_doctype can automatically send Content-Type headers to the
 *             browser. To disable set parameter "sendheaders" to FALSE.
 *
 * Author:   André Rabold
 * Contribs: S. Shah
 * Idea:     Peter Turcan
 *           Monte Ohrt
 *           Matthias Mohr
 * Modified: 2004/04/23
 * Version:  1.5
 * -------------------------------------------------------------
 */
function smarty_function_html_doctype($params, &$smarty)
{
  // Default values:
  $xhtml            = true;
  $version         = 4.01; // added by k-dub, was undefined when $xhtml = false
  $type             = "Transitional";
  $dtd              = true;
  $omitxml          = false;
  $encoding         = "UTF-8";
  $sendheaders      = true;
  $force_opera_html = false;
 
  extract($params);
 
  $type = ucfirst( strtolower($type) ); // standardise $type's case
  if (headers_sent())
    $sendheaders = false;

  // DOCTYPE Header 
  $header = "";

  // Impliment opera check and overide
  if($force_opera_html && $xhtml)
  {
      if (stristr($_SERVER['HTTP_USER_AGENT'],"Opera 7")||stristr($_SERVER['HTTP_USER_AGENT'],"Opera/7"))
      {
          $xhtml = false;
          $version = 4.01;
      }
  }
 
 
  // XHTML
  if ($xhtml) {
    if ($sendheaders) {
      // first check if browser accepts application/xhtml+xml content type
      if (stristr($_SERVER['HTTP_ACCEPT'],"application/xhtml+xml")) {
        header("Content-Type: application/xhtml+xml; charset=$encoding");
        $GLOBALS["HTML_DOCTYPE"] = "xml";//******
      }
      elseif (stristr($_SERVER['HTTP_ACCEPT'],"application/xml")) {
        header("Content-Type: application/xml; charset=$encoding");
        $GLOBALS["HTML_DOCTYPE"] = "xml";//******
      }
      elseif (stristr($_SERVER['HTTP_ACCEPT'],"text/xml")) {
        header("Content-Type: text/xml; charset=$encoding");
        $GLOBALS["HTML_DOCTYPE"] = "xml";//******
      }
      //Send Opera 7.0 application/xhtml+xml
      elseif (stristr($_SERVER['HTTP_USER_AGENT'],"Opera 7")||stristr($_SERVER['HTTP_USER_AGENT'],"Opera/7")) {
        header("Content-Type: application/xhtml+xml; charset=$encoding");
        $GLOBALS["HTML_DOCTYPE"] = "xml";//******
      }
      //Send everyone else text/html
      else {
        header("Content-Type: text/html; charset=$encoding");
        $GLOBALS["HTML_DOCTYPE"] = "html";//******
      }
    }
   
    if (!isset($version) || $version > 1.1) {
      $version = "1.0";  // default version for XHMTL
   }
    if ($dtd === true) {
      // Add default DTD
      if ($version == "1.0" && strtolower($type) == "strict")
        $dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
      else if ($version == "1.0" && strtolower($type) == "transitional")
        $dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";
      else if ($version == "1.0" && strtolower($type) == "frameset")
        $dtd = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd";
      else if ($version == "1.1")
        $dtd = "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";
      else
        $dtd = ""; // unknown XHTML version and/or type
    }

    if (!$omitxml) {
      if (trim($encoding) != "")
        $header .= "<?xml version=\"1.0\" encoding=\"$encoding\"?>\n";
      else
        $header .= "<?xml version=\"1.0\"?>\n";
    }
   
    // For some reasons "html" is written in lowercase here (check w3.org)
    if ($version == "1.1")
      $header .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"";
    else //default
      $header .= "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML $version $type//EN\"";

    if ($dtd != "" && $dtd !== false)
      $header .= " \"$dtd\"";
    $header .= ">\n";
  }
  // HTML
  else {
    if ($sendheaders) {
      header("Content-Type: text/html; charset=$encoding");
        $GLOBALS["HTML_DOCTYPE"] = "html";
    }
   
    if ($version == "2.0" || intval($version) == 2) {
      // does anybody still uses this?
      $header .= "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n";
    }
    else if ($version == "3.2" || intval($version) == 3) {
      // does anybody still uses this?
      $header .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n";
    }
    else {
      if (!isset($version))
        $version = "4.01"; // default version for HTML
      elseif ($version == "4.0" || $version == "4.00")
        $version = "4.0";
     
      if ($dtd === true) {
        // Add default DTD
        if ($version === "4.01" && (strtolower($type) == "strict" || empty($type)))
          $dtd = "http://www.w3.org/TR/html4/strict.dtd";
        elseif ($version === "4.01" && strtolower($type) == "transitional")
          $dtd = "http://www.w3.org/TR/html4/loose.dtd";
        elseif ($version === "4.01" && strtolower($type) == "frameset")
          $dtd = "http://www.w3.org/TR/html4/frameset.dtd";
        elseif ($version === "4.0" && (strtolower($type) == "strict" || empty($type)))
          $dtd = "http://www.w3.org/TR/REC-html40/strict.dtd";
        elseif ($version === "4.0" && strtolower($type) == "transitional")
          $dtd = "http://www.w3.org/TR/REC-html40/loose.dtd";
        elseif ($version === "4.0" && strtolower($type) == "frameset")
          $dtd = "http://www.w3.org/TR/REC-html40/frameset.dtd";
        else
          $dtd = ""; // no default DTD (e.g. version 3.2 and 2.0 doesn't use a DTD)
      }
     
      if (empty($type) || strtolower($type) == "strict")
        $header .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML $version//EN\"";
      else
        $header .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML $version $type//EN\"";
      if ($dtd != "" && $dtd !== false)
        $header .= " \"$dtd\"";
      $header .= ">\n";
    }
  }
   
  return $header;
}
?>



I added the version default at the top of the script, because it was undefined if $xhtml=false.

messju wrote:
Code:
{html_doctype force_opera_html=true}<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">


I tried this, but the error is before the html tag. XML requires that
Code:
<?xml version="1.0"?>
be the first line of the document, if specified.
_________________
rm -rf /bin/laden
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Fri Oct 01, 2004 7:06 am    Post subject: Reply with quote

k-dub wrote:
I tried this, but the error is before the html tag. XML requires that
Code:
<?xml version="1.0"?>
be the first line of the document, if specified.


then you have a trailing (or leading) newline in any of the php-files you include.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
sage
Smarty Rookie


Joined: 09 May 2004
Posts: 20
Location: London, UK

PostPosted: Fri Oct 01, 2004 9:52 am    Post subject: Reply with quote

It may be worth just examining what the function is returning, on its own:

instead of the end of the function use somthing like:

[php:1:d9f06e33d0]


die("-".$header."-");
// return $header;
}
?>[/php:1:d9f06e33d0]

Put the "-" to know exactly the string being returned when looking at the source.
Hopefully that will help narrow down the problem.
Back to top
View user's profile Send private message
k-dub
Smarty Rookie


Joined: 30 Sep 2004
Posts: 9
Location: Land of Opportunity, State of Confusion

PostPosted: Sat Oct 02, 2004 6:03 am    Post subject: Reply with quote

OK, I tried that. With the template source starting like this:
Code:
{html_doctype version=1.1 force_opera_html=true}
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

I get:
Code:

-<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

Is there somewhere else I should be looking?
_________________
rm -rf /bin/laden
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
k-dub
Smarty Rookie


Joined: 30 Sep 2004
Posts: 9
Location: Land of Opportunity, State of Confusion

PostPosted: Sat Oct 02, 2004 6:27 am    Post subject: Reply with quote

I just tried something interesting. I changed
Code:
$smarty->display( $page . ".tpl" );
to
Code:
$result = $smarty->fetch( $page . ".tpl" );
echo( "<?xml version=\"1.0\"?>\n<root>\n<![CDATA[" . $result . "]]>\n</root>" );
in my index.php file.

Here's how the result starts:
Code:
<?xml version="1.0"?>
<root>
<![CDATA[
-<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">


It appears the newline is being insterted by some piece before the plugin output comes in. For the record, the compiled template starts with:
Code:
<?php /* Smarty version 2.6.5-dev, created on 2004-10-01 23:39:40
         compiled from home.tpl */ ?>
<?php require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.load_plugins.php');
smarty_core_load_plugins(array('plugins' => array(array('function', 'html_doctype', 'home.tpl', 1, false),)), $this);?>
<?php echo smarty_function_html_doctype(array('version' => 1.1,'force_opera_html' => true), $this);?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

_________________
rm -rf /bin/laden
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
k-dub
Smarty Rookie


Joined: 30 Sep 2004
Posts: 9
Location: Land of Opportunity, State of Confusion

PostPosted: Sun Oct 10, 2004 4:55 am    Post subject: Reply with quote

I had a chance to install the same versions of Apache, mod_php, and Smarty on a Gentoo Linux box, and everything works fine. Question There is definitely a Windows-specific error, but the trick is finding out if it is in Apache, PHP. or Smarty. It still seems like a problem with the html_doctype plugin, but I don't see it.
_________________
rm -rf /bin/laden
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Plugins All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP