Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to format HTML in PHP templates #845

Closed
claytonrcarter opened this issue Nov 19, 2018 · 5 comments
Closed

How to format HTML in PHP templates #845

claytonrcarter opened this issue Nov 19, 2018 · 5 comments

Comments

@claytonrcarter
Copy link
Contributor

Prettier 1.15.2
PHP Plugin 0.9.0

Now that prettier supports formatting HTML, how can we have plugin-php format PHP and HTML in a PHP file?

Input:

<?php
$somePoorly    = 'formatted'. 'code' .
'right'       .
 ( isset( $nope) ? 'here'
:
'and here'
)
;

?>
<html
><head

>

</head>
<body>
More
bad
code here.
</body
>

</html>

Output of prettier test.php: (PHP is properly formatted, HTML is not.)

<?php
$somePoorly =
    'formatted' . 'code' . 'right' . (isset($nope) ? 'here' : 'and here'); ?>
<html
><head

>

</head>
<body>
More
bad
code here.
</body
>

</html>

Output of prettier test.php --parser html: (HTML is properly formatted, PHP is not.)

<?php $somePoorly = 'formatted'. 'code' . 'right' . ( isset( $nope) ? 'here' :
'and here' ) ; ?>
<html>
  <head> </head>
  <body>
    More bad code here.
  </body>
</html>

Expected behavior: (Both PHP & HTML are properly formatted.)

<?php
$somePoorly =
    'formatted' . 'code' . 'right' . (isset($nope) ? 'here' : 'and here'); ?>
<html>
  <head> </head>
  <body>
    More bad code here.
  </body>
</html>
@alexander-akait
Copy link
Member

alexander-akait commented Nov 19, 2018

It is unsafe, we can't format code like this:

<<?php echo 'img'; ?> src="http://example.com/image.png" <?php echo attributes(); ?>>`

@alexander-akait
Copy link
Member

alexander-akait commented Nov 19, 2018

So i don't think it is good idea enable this, also we have a lot of problems with inline nodes and their formatting

@claytonrcarter
Copy link
Contributor Author

I understand that this may be too messy or difficult to implement. Some questions:

I believe the HTML parser supports formatting JS w/i Vue templates and <script> tags and such. How is this different?

Is there a way for this plugin to instruct the html parser to ignore PHP "nodes"? This way the HTML parser might format the HTML without mangling the PHP (as in the example output above).

Thanks!

@adamroyle
Copy link

@claytonrcarter Vue templates can be parsed by the html parser because they are html-compliant.

PHP can't be parsed by the html parser because php is not html-compliant.

And html can't be parsed by php parser since it's not valid php.

Using a combination of parsers (php for php blocks and html for text nodes) is impossible for the majority of cases where php is intermingled with html like evilebottnawi's example because the text node is not valid html on its own, it requires the output from php to be valid.

Another reason is text nodes might not actually be html, it could literally be any type of arbitrary text.

@claytonrcarter
Copy link
Contributor Author

Fair enough. This all makes complete sense and I'm realizing that this is probably the wrong place for this issue anyway. I found this issue on the main Prettier repo with some context on HTML templates: prettier/prettier#5428

andi34 added a commit to andi34/photobooth that referenced this issue Oct 30, 2020
- Please note: php-plugin can't handle files with mixed content (PHP containing HTML and/or JS and/or CSS).
  For reference:
  - prettier/plugin-php#846
  - prettier/plugin-php#845

Change-Id: I40f2f013ec8f9f9557285f38bdc78190d7fb5b41
andi34 added a commit to andi34/photobooth that referenced this issue Dec 21, 2020
- Please note: php-plugin can't handle files with mixed content (PHP containing HTML and/or JS and/or CSS).
  For reference:
  - prettier/plugin-php#846
  - prettier/plugin-php#845

Change-Id: I40f2f013ec8f9f9557285f38bdc78190d7fb5b41
andi34 added a commit to andi34/photobooth that referenced this issue Dec 23, 2020
- Please note: php-plugin can't handle files with mixed content (PHP containing HTML and/or JS and/or CSS).
  For reference:
  - prettier/plugin-php#846
  - prettier/plugin-php#845

Change-Id: I40f2f013ec8f9f9557285f38bdc78190d7fb5b41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants