0

I have two PHP files, that are used as Views in an MVC-based application. Both of them contain mostly HTML, but also some variables sent to them by the Controller.

maintemplate.php:

<!DOCTYPE html>
<html>
    <head>
        <title><?php echo $viewModel->get('pageTitle'); ?></title>
    </head>
    <body>
        <?php require($this->viewFile); //index.php ?>
    </body>
</html>

index.php:

<ul>
    <li><a href="fanfics/list">fanfics list</a></li>
</ul>

The problem is that the code shown in "View Source" (I checked Chrome, IE, and FF) is not nested as I would expect it to be. The first line is nested correctly - four spaces after the containing tag, but the other lines are nested as if the first line has no spaces before it at all.

View Source:

<!DOCTYPE html>
<html>
    <head>
        <title>Fanfiction application</title>
    </head>
    <body>
        <ul> <!-- 4 spaces after the body -->
    <li><a href="fanfic/list">fanfics list</a></li> <!-- No spaces after the body -->
</ul>    
    </body>
</html>

I tried to search on Google and on StackOverflow various variants of bad nesting view source, but none of the results seems to answer my question.

So how can I solved that? Thank you.

Michael Haddad
  • 4,085
  • 7
  • 42
  • 82
  • `include` just literally outputs whatever is in your file. It does not add any spaces. The first lines´ spaces just stem from the indented ` – mario Oct 11 '15 at 08:54
  • 1
    Why do you want to "solve" that? It is not an issue at all. HTML markup ignores stray white spaces except inside value properties. – arkascha Oct 11 '15 at 08:56
  • mario - The thing is that I really think `require` is more appropriate here. That page is essential and I need to stop the application if it is not imported. arkascha - I believe everyone should be able to use your HTML. That's one of the basics of modern development, I believe. I want my code to be shown properly for those who wants to use it. – Michael Haddad Oct 11 '15 at 08:57
  • 1
    require & include handle the inclusion of file output the same, in terms of layout, spacing etc, the only difference is whether the file is essential (`require`) or not (`include`) – Rob G Oct 11 '15 at 09:06
  • also, speaking from experience, be wary of making your output easier to read for people, if it comes at the expense of the readability of your own code - after all, it's you that'll need to read & understand it further down the line :) – Rob G Oct 11 '15 at 09:08
  • Rob, I totaly agree with you. Could you please help me to make my code more readable for others without making it unreadable for myself? Because the answer I was given by Darren H requires that I make the orginial code messy for the "View Source" to show it correctly. – Michael Haddad Oct 11 '15 at 09:10
  • 2
    run the resulting page through an output processor like tidy or htmlpurifier, as suggested in the linked dupe. – Gordon Oct 11 '15 at 09:11
  • @sipo, yeah, agree with Gordon - throw it all through a tidier/purifier if you want pretty output – Rob G Oct 11 '15 at 09:12
  • What do you mean nesting is incorrect? The nesting is correct by all means—it is the **indentation** that is unintuitive. But indentation has nothing to do with the validity (semantics or otherwise) of your code—why do you have to prettify the source? – Terry Oct 11 '15 at 09:19

1 Answers1

1

As explained by Mario in the comments...

maintemplate.php:

<!DOCTYPE html>
<html>
    <head>
        <title><?php echo $viewModel->get('pageTitle'); ?></title>
    </head>
    <body>
<?php require($this->viewFile); //index.php ?>
    </body>
</html>

index.php:

        <ul>
            <li><a href="fanfics/list">fanfics list</a></li>
        </ul>

Then your view source will display as you want.

Look at your version of index.php, line 2, is indented 4 spaces, that is what appears in view source. Look at line 3, indented 0 spaces, as seen in view source. This is rectified in the new version by indenting them the amount of spaces you want.

As for line 1, in your index.php it is not indented any spaces so it appears exactly where the require part starts in maintemplate.php (already indented 8 spaces)

Requiring a file does not alter the indentation in any way, it just combines what you have specified in your files.

In the version above, all the lines in index.php are indented manually to match where they will be positioned in the source view, you just need to remember to not indent the require line because any indentation there will be added to the indentation on line 1 in index.php

Darren H
  • 910
  • 8
  • 24
  • That way, the code will be shown correctly in the "View Source", but not on the server files which I use, and that is far worse I believe. – Michael Haddad Oct 11 '15 at 09:08
  • I have modified my answer, I hope that is a bit clearer – Darren H Oct 11 '15 at 09:12
  • It is more clear, thanks. But it still does not precisely what I need. I want to be able to see the code both in the server and the "View Source" with no extra indentation. Thanks. – Michael Haddad Oct 11 '15 at 17:43
  • There isn't any extra indendation – Darren H Oct 11 '15 at 17:45
  • Well, in your answer, in `index.php` there are some spaces before the `
      `. Those are the spaces I am talking about. I am sorry of I was not clear.
    – Michael Haddad Oct 11 '15 at 17:57
  • You want to have 8 spaces in front of UL in the source, don't you? – Darren H Oct 11 '15 at 18:05
  • I do, but I do not want it to be at the expense of the code clarity in the sever. I am searching for a solution that allows me to write code with clear indention to both me and the user (i.e. the "View Source"). Thank you. – Michael Haddad Oct 11 '15 at 18:11