format-html-in-php icon indicating copy to clipboard operation
format-html-in-php copied to clipboard

Html and php tags dont indent correctly in php loops

Open justlevine opened this issue 5 years ago • 6 comments

Hi, Ive got two separate issues when dealing with code in php loops:

First, the html tags ignore the loops entirely. Second, the php tags get indented incorrectly, since they look for the last html tag.

Ex. what I get:

<?php
	$var = getVar();
if ( $var ) { 
	if ( $var2 ) {
		?>
<p>Var 2 is true</p>
<?php
	}
	?>
<div>
	<p>Var 1 is true</p>
</div>
<?php
}

What it should look like:

<?php
	$var = getVar();
	if ( $var ) {
		if ( $var2 ) {
		?>
			<p>Var 2 is true</p>
			<?php
		}
		?>
		<div>
			<p>Var 1 is true</p>
		</div>
	<?php
}

Suggestions?

justlevine avatar May 03 '19 20:05 justlevine

Same here! A fix would be great.

rcobiella avatar May 08 '19 15:05 rcobiella

Hey if you don't mind give me a specific example with a before and after. You can test it yourself also at https://beautifier.io/ this plugin just uses that library, change the dropdown to HTML.

This is what I see.

Before

<?php
	$var = getVar();
	if ( $var ) {
		if ( $var2 ) {
		?>
			<p>Var 2 is true</p>
			<?php
		}
		?>
		<div>
			<p>Var 1 is true</p>
		</div>
	<?php
}

After

<?php
	$var = getVar();
	if ( $var ) {
		if ( $var2 ) {
		?>
<p>Var 2 is true</p>
<?php
		}
		?>
<div>
	<p>Var 1 is true</p>
</div>
<?php
}

You could also just not break out of PHP and echo out the HTML, which doesn't mess with the formatting.

I guess I would need to know what you expect, should the HTML tag always respect the indentation of the closing PHP tag above it or how should it know where to start indentation? Currently there is no other HTML tags so it doesn't understand why it's indented so far and moves itself to 0 indentation.

RiFi2k avatar Aug 24 '19 20:08 RiFi2k

Closing this unless someone wants to chime in about the expectations of it.

RiFi2k avatar Jan 18 '20 07:01 RiFi2k

Apologies - didnt see the request for suggestions.

Unsure how beautifier.io works, but ideally html tags should just inherit the php indentation, while ignoring <?php and ?> tags that are on their own line.

E.g.

<?php

$i = 0; // php opening tag is ignored. My choice to indent or not.

if ($i === 0; $i <1; $i++){ // matches previous php statement, not the php opening tag.
	if( $i === 4):
		?>
		<!-- Indented 2x because its in a nested if statement -->
		<span class="special">
	        	<?php echo '$i equals 4'; // indented inside the `span` tag?>  
      		</span>
	<?php endif; ?>

	<!-- Indented 1x because its in the parent if statement -->
	<div class="class-<?php echo $i">
		<?php echo 'This is div #' . $i; // indented 1x inside the div tag ?>
	</div>
} // matches if statement
?>

<div class="footer">
	<?php //indented 1x since its inside div tag
		$display_footer_text = true; //I chose to indent it in the php tag, so leave it indented.
		
		if( $display_footer_text ){ // indented to match previous php statement
			?><!-- indented because its inside the if statement -->

			<!-- closing php tag ignored, indented because inside if statement. -->
  			<span>
				<?php echo 'Footer Text'; //indented inside span?>
			</span>
		<?php else { // matches if statement ?>
  			<span> No footer text to display </span>
	  		<?php // matches the span statement
		} // matches the else statement
	//not part of a loop, so match it with the opening php statement ?>
</div>

Throwing this into beautifier.io and we get a mess:

<?php

$i = 0;

if ($i === 0; $i <1; $i++){ 
	if( $i === 4):
		?>
<span class="special">
    <?php echo '$i equals 4'; ?>
</span>
<?php endif; ?>

<div class="class-<?php echo $i">
		<?php echo 'This is div #' . $i; ?>
	</div>
}
?>

<div class=" footer">
    <?php 
		$display_footer_text = true;
		
		if( $display_footer_text ){
			?>

    <span>
        <?php echo 'Footer Text'; ?>
    </span>
    <?php else {  ?>
    <span> No footer text to display </span>
    <?php
		} 
	?>
</div>

justlevine avatar Jan 21 '20 01:01 justlevine

Well I'm about to roll out the newest version they released and now they added in the ability to add in your template type which we can set to PHP and also they have a few new PHP specific fixes. Also I'll just set the templating type to PHP globally because you couldn't currently in VSCode.

So on your example it looks like you missed a PHP ending tag and that probably effected your output a lot if you fix this part where you don't close the <?php echo $i">.

	<div class="class-<?php echo $i">
		<?php echo 'This is div #' . $i; // indented 1x inside the div tag ?>
	</div>
} // matches if statement
?>

Then using these settings pasting them in manually in the textarea and changing it to HTML on the dropdown on beautifier.io

{
  "indent_size": 4,
  "indent_char": " ",
  "max_preserve_newlines": 10,
  "preserve_newlines": true,
  "keep_array_indentation": true,
  "break_chained_methods": false,
  "indent_scripts": "keep",
  "brace_style": "collapse",
  "space_before_conditional": true,
  "unescape_strings": true,
  "jslint_happy": false,
  "end_with_newline": true,
  "wrap_line_length": 0,
  "indent_inner_html": false,
  "comma_first": false,
  "e4x": false,
  "indent_empty_lines": true,
  "templating": "php",
  "indent_handlebars": true
}

This will give you what your output will be when I roll the new version.

Once we can get the baseline then I'll be open to talking about how we could setup keeping / not keeping indentation inside PHP tags and also the initial indentation on a template type file which won't have the initial body HTML so it won't know where it's supposed to start so it just starts at 0.

RiFi2k avatar Jan 21 '20 02:01 RiFi2k

@justlevine above message so you get notified.

RiFi2k avatar Jan 21 '20 02:01 RiFi2k