sublimetext-codeformatter icon indicating copy to clipboard operation
sublimetext-codeformatter copied to clipboard

PHP formatting with HTML

Open ghost opened this issue 10 years ago • 10 comments

Hello, is possible add something that don't break php while formatting in HTML or that don't brake html when formatting in PHP? Is a pain every time get formatted got both?

ghost avatar Aug 28 '15 08:08 ghost

CodeFormatter aimed to good code and good code is PHP without HTML inside and reverse :)

akalongman avatar Aug 28 '15 09:08 akalongman

+1 for formating html inside php files e.g. WordPress have php + html code in templates ;)

PixelT avatar Dec 22 '15 09:12 PixelT

@PixelT @Guich sorry guys but this is near to impossible to correctly format php+html (+css+js)

there are some parsers that can handle this type of code (sublime text syntax highlighter does that) but it is very complicated to format that

people can come up with very weird stuff like this

<script>
<? foreach($a as $b) { ?>
document.write("<?= $b ?>")
<? } ?>
</script>

and how the hell we are going to format this ?

thecotne avatar Jan 12 '16 18:01 thecotne

CodeFormatter aimed to good code and good code is PHP without HTML inside and reverse :)

There is also a problem of context. In PHP, you never know what lives outside the closing tag (?>), it can be anything - including binary. That's why there is now way to safely mix PHP and any other formatter.

ucirello avatar Jan 21 '16 12:01 ucirello

Just wondering how is this impossible if php tags are always opened & closed properly? Nearly all my jobs have files with both languages...

v3nt avatar May 17 '16 09:05 v3nt

@v3nt you need to use some template engine instead of mixing php with html and then it will be simpler to fix formatting

thecotne avatar May 17 '16 11:05 thecotne

for new jobs that's fine but i've got 10 years worth of older jobs where its mixed. Esp Wordpress sites.

v3nt avatar May 17 '16 16:05 v3nt

@v3nt we don't have this feature not because we don't want to but because it is near to impossible and very difficult

thecotne avatar May 17 '16 17:05 thecotne

This should be re-opened as it is common issue with the mixed code and codeformatter is unable to cope with it. Even worse what happens is that codeformatter tries to format PHP along HTML code and actually introducing syntax errors!

The solution instead is to turn off formatting completely for such document, if HTML is encountered.

Even better solution would be that codeformatter would tokenize the code and check starting <?php and ending ?> tags and format just the code in between. This is hardly something that is impossible to implement.

nekromoff avatar Dec 04 '17 13:12 nekromoff

@nekromoff feel free to send pull request

thecotne avatar Dec 05 '17 06:12 thecotne