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.

9
  • include just literally outputs whatever is in your file. It does not add any spaces. The first lines´ spaces just stem from the indented <?php before the require(). Commented Oct 11, 2015 at 8: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. Commented Oct 11, 2015 at 8: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. Commented Oct 11, 2015 at 8: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) Commented Oct 11, 2015 at 9:06
  • 2
    run the resulting page through an output processor like tidy or htmlpurifier, as suggested in the linked dupe. Commented Oct 11, 2015 at 9:11

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

7 Comments

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.
I have modified my answer, I hope that is a bit clearer
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.
There isn't any extra indendation
Well, in your answer, in index.php there are some spaces before the <ul>. Those are the spaces I am talking about. I am sorry of I was not clear.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.