Forum

Thread tagged as: Question

Maintaining Code Formatting

Is it possible to maintain code formatting while using Perch? I was careful to indent lines and keep my code clean but after implementing Perch on my site, when I view the source, things are quite a mess. Is this normal? Or is there a way I could maintain the formatting by indenting templates the same way etc?

Trevor Brandt

Trevor Brandt 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you give an example?

Sure. Maybe it isn't a big deal or anything? Just wanted to check. Here's a small example:

Viewing page source:

        <footer>
            Copyright © Mutual Memoir 2016.        </footer>

Before Perch:

        <footer>
            <p>&copy; 2016 Mutual Memoir.</p>
        </footer>

Or the nav:

            <h1>MM</h1>
            <nav>

    <ul>

        <li class="selected">
            <a href="/">Home</a>   

        </li>

        <li>
            <a href="/about.php">About</a>   

        </li>

    </ul>
            </nav>

Before:

            <nav>
                <ul>
                    <li><a href="index.html">Home</a></li>
                    <li><a href="#">About</a></li>
                    <li><a href="#">Login</a></li>
                </ul>
            </nav>
Drew McLellan

Drew McLellan 2638 points
Perch Support

It looks like there are spaces where the Perch tags have been removed. We don't remove any of your template when we remove standard tags, so if there's whitespace surrounding them then it'll remain.

You can either bunch your templates up around the Perch tags, or another option is to use template comments, which do swallow surrounding whitespace.

Of course, the reason have your source code neat is that it helps you in debugging and maintenance. It's not for the browser - the browser doesn't care. It's not for someone else viewing source on your site - they're using tools that reformat the code anyway. In this case, the code you're needing to keep neat to be easy to work with is the template code.

So I personally would keep my templates neat and not worry about the whitespace in the HTML, because it's really of no consequence.

Awesome, thanks for the detailed explanation. Appreciated!