plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

Format PHP + HTML + JS + CSS at once? (Wordpress)

Open kkmuffme opened this issue 6 years ago • 16 comments

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.

kkmuffme avatar Nov 26 '18 20:11 kkmuffme

related discussion in https://github.com/prettier/plugin-php/issues/845

hawkrives avatar Nov 26 '18 21:11 hawkrives

also relevant: https://github.com/prettier/prettier/issues/5428

claytonrcarter avatar Nov 26 '18 23:11 claytonrcarter

It is dangerous and very hard, because PHP is not compatibility with html, css and etc. Better avoid using inline in PHP.

alexander-akait avatar Nov 27 '18 10:11 alexander-akait

@kkmuffme I made this to get the HTML - https://marketplace.visualstudio.com/items?itemName=rifi2k.format-html-in-php

RiFi2k avatar Dec 06 '18 17:12 RiFi2k

@RiFi2k maybe you can help to us implement this feature? We can implement this using option to avoid break code by default.

alexander-akait avatar Dec 06 '18 18:12 alexander-akait

@evilebottnawi I'm down, you would be looking to add HTML as an optional add on?

RiFi2k avatar Dec 06 '18 18:12 RiFi2k

@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)

alexander-akait avatar Dec 06 '18 18:12 alexander-akait

@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.

RiFi2k avatar Dec 06 '18 18:12 RiFi2k

@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 avatar Dec 06 '18 18:12 RiFi2k

@RiFi2k great, when i will have time and opportunities i will look your code, anyway any help welcome :+1:

alexander-akait avatar Dec 06 '18 18:12 alexander-akait

@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 avatar Dec 06 '18 18:12 RiFi2k

@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.

alexander-akait avatar Dec 06 '18 19:12 alexander-akait

@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.

RiFi2k avatar Dec 06 '18 19:12 RiFi2k

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?

RiFi2k avatar Dec 06 '18 19:12 RiFi2k

Was this ever implemented? I would be more than grateful if it was.

jeslenbucci avatar May 13 '20 20:05 jeslenbucci