plugin-php
plugin-php copied to clipboard
Format PHP + HTML + JS + CSS at once? (Wordpress)
Will it be/is it possible when using prettier + the coming PHP plugin, to prettify a .php file that contains php + html + inline js (
Atm I am using phpcs + eslint at the moment, which struggle and output a complete chaos of indentation unless it was already correct (& even then they sometimes mess it up, if its a mixed file, like it's often encountered in WP core & plugins)
I am available for testing & can supply some test files that were absolutely butchered by phpcsfixer/eslint, just let me know how I can help.
related discussion in https://github.com/prettier/plugin-php/issues/845
also relevant: https://github.com/prettier/prettier/issues/5428
It is dangerous and very hard, because PHP
is not compatibility with html
, css
and etc. Better avoid using inline
in PHP
.
@kkmuffme I made this to get the HTML - https://marketplace.visualstudio.com/items?itemName=rifi2k.format-html-in-php
@RiFi2k maybe you can help to us implement this feature? We can implement this using option to avoid break code by default.
@evilebottnawi I'm down, you would be looking to add HTML as an optional add on?
@RiFi2k prettier
already support html
we need implement supporting html under flag because it can be dangerous (right now, maybe we can use this behavior by default in future)
@evilebottnawi I was actually about to take a look at using https://github.com/Prettyhtml/prettyhtml instead of https://github.com/beautify-web/js-beautify so that fits with what your saying.
@evilebottnawi PS: for me personally I have never had my implementation ever actually break any PHP code and to my knowledge nobody has ever claimed about that happening while using my extension
@RiFi2k great, when i will have time and opportunities i will look your code, anyway any help welcome :+1:
@evilebottnawi Basically initially I swapped the open and closing PHP tags for comment tags, then ran the whole block through js-beautify html format and then went back and changed it back to PHP tags.
Then after finding a bug in a rc release of js-beautify one of the maintainers told me that it would ignore all PHP code anyways, so it was kind of a waste to do what I was doing, so from then on I've passed the whole block PHP and all through with no problems.
I always run my hook before the PHP formatting happens.
On a side note did you have any plans for a VScode extension? My ultimate goal was to have what I have but add the option to also enable phpcs and also phpcbf or prettier/prettier-php to format the PHP code, so I would register the extension as a proper formatting provider instead of what I need to do which is run on a hook before those run.
@RiFi2k I think besic prettier
plugin should support PHP
by default, so we can reformat php
and html
out of box, also we can implement reformat css
and js
inside php in future.
@evilebottnawi So then it's more of a thing where prettier vscode needs to lock down an easy way to include prettier plugins (most likely locally installing prettier and the plugin with npm/yarn) and turning on a prettier.php config setting so it knows to register for PHP files and run.
For the HTML, CSS inside of a PHP file, how would that work as far as being able to configure options on how it is formatted. Or would prettier basically pick the settings internally like it currently does and allow minimal config?
Was this ever implemented? I would be more than grateful if it was.