pug-php / pug

Pug template engine for PHP
https://www.phug-lang.com
MIT License
391 stars 42 forks source link

Pug-PHP 3: Several rendering issues #167

Closed wolfgang42 closed 7 years ago

wolfgang42 commented 7 years ago

All of the following work as expected with version 2.7 and fail on 3.0.0-beta1. They can be reproduced with the following code:

<?php
require __DIR__.'/../vendor/autoload.php';
use Pug\Pug;
$pug = new Pug([
    'basedir' => __DIR__.'/../views/',
    'cache' => '/tmp/',
]);
echo $pug->renderFile('index');

Improper nesting with unbuffered comment

div
    div
        //- lorem
    ul
        li Item

Expected:

<div><div></div><ul><li>Item</li></ul></div>

Got:

<div><div><ul></ul><li>Item</li></div></div>

Incorrect parsing of classes beginning with digits

.4u content

Expected:

<div class="4u">content</div>

Got:

.4u content
wolfgang42 commented 7 years ago

Additionally, the following fails if PHP's short open tags are enabled:

doctype xml

with the error Uncaught ParseError: syntax error, unexpected 'version' (T_STRING).

There appears to be a workaround for this included in version 2 which didn't make it into version 3; the generated code looks like this:

<<?php echo '?xml version="1.0" encoding="utf-8" ?'; ?>>
kylekatarnls commented 7 years ago

The XML doctype bug is a tricky problem for us and we are aware of it.

First of all, you can easily work around, see our HHVM compatible test, we use the following custom format: https://github.com/phug-php/renderer/blob/master/tests/Phug/Utils/XmlHhvmFormat.php With the new architecture, you can extend any doctype format and override their settings. Then you can specify your custom format in the options: https://github.com/phug-php/renderer/blob/master/tests/Phug/AbstractRendererTest.php#L72 Or the complete example for pug-php (we're preparing the documentation for all those kind of stuff)

<?php

use Phug\Formatter\Format\XmlFormat;
use Pug\Pug;

require __DIR__.'/../vendor/autoload.php';

class XmlShortTagFormat extends XmlFormat
{
    const DOCTYPE = '<<?= "?" ?>xml version="1.0" encoding="utf-8" ?>';
}

$pug = new Pug([
  'formats' => [
    'xml' => XmlShortTagFormat::class,
  ],
]);

Now to understand why it's no longer the default XML doctype, our strategy is to provide clean and easy to read patterns as settings. And pug-php is now able to output many format (not necessarily compatible, you can output twig, angular or any kind of tags templates) Right now, xml settings are:

    const DOCTYPE = '<?xml version="1.0" encoding="utf-8" ?>';
    const OPEN_PAIR_TAG = '<%s>';
    const CLOSE_PAIR_TAG = '</%s>';
    const SELF_CLOSING_TAG = '<%s />';
    const ATTRIBUTE_PATTERN = ' %s="%s"';
    const BOOLEAN_ATTRIBUTE_PATTERN = ' %s="%s"';

Those settings are PHP-agnostic and easy to read/reuse (you can now use XmlFormat::DOCTYPE, to display, to compare, etc.).

If we put the short-tag compatible, we will loose those advantages.

Another point is, short tags (except echo short tags) have always been a bad idea because of the XML conflict. That's why it is most of time disable and why it has been removed from PHP since 7.0. PHP 5.6 is the last PHP 5 version maintained. So we prefer provide some work-around rather than support obsolete feature.

You are encouraged to pass to PHP 7.1 or at least disable short_tag settings. It's a very better way to handle it.

I will fix the digit-class and comment indent problems.

kylekatarnls commented 7 years ago

Hi, digit-class are now allowed, you can update to get it working.

So now left the problem with indented comments. It's something wrong about the comment scanner in the lexer.

Status of your requests:

Request Status
doctype xml with short-tags enabled won't fix, please update to PHP 7 or disable short-tag or use the work-around
class names starting with digit fixed
nested comments in tag break indent fixed
kylekatarnls commented 7 years ago

Hi, outdent after indented comments have been fixed in the phug lexer. Please use composer update to get the new version.

Thanks a lot for your help. I will discuss about short-tags with @TorbenKoehn, I'm considering detecting if short-tags are enable and escape doctypes if so in an automated way. But I really encourage you to step in PHP 7 and hope you approve.

wolfgang42 commented 7 years ago

Thanks, I can confirm that the rendering issues have been fixed. (Interestingly, version 3 seems to preserve newlines with 'prettyprint'=>false in places where version 2 wouldn't have.)

Regarding the short-tags, I agree with your position (they weren't supposed to be enabled on the server anyway); I just thought I'd mention it since I didn't see anything in the change-log and it took me a while to track down the error.

kylekatarnls commented 7 years ago

XML doctype escape has been restored via a setting: https://github.com/phug-php/formatter/releases/tag/0.5.28