I'm trying to figure out how to maintain the proper indentation when rendering HTML content that I've pulled from a database. Currently, the content includes raw HTML code that looks like this when rendered:
<html>
<head></head>
<body>
<!-- the following is read from a database -->
<h1>A title from a database</h1>
<p>A paragraph from a database</p>
</body>
However, I'd like for it to look properly indented like this:
<html>
<head></head>
<body>
<!-- the following is read from a database -->
<h1>A title from a database</h1>
<p>A paragraph from a database</p>
</body>
Is it possible to have a script that adds the necessary indentation or are there other better solutions?
2 Answers
That's an interesting question! While the indentation won't really affect how the HTML renders visually, preserving it can definitely help keep your source code tidy. It makes debugging and future edits easier since you can quickly spot issues in the structure. That said, if the content is auto-generated and won't be manually edited later, it might not be worth the effort. You could use a script that adjusts the indentation when saving to the database, though it may complicate things.
I've noticed that HTML indentation can help a lot for readability, especially during debugging. Keeping the HTML structured helps you spot any errors quickly. Even if it’s just for the developer’s eyes, proper indentation can save time later. If the content's coming from a database and is auto-rendered, you may want to consider adding some logic to handle indentation dynamically as you save it.
Totally! I've been there myself, obsessing over indentation. Just remember, as long as it works, you don't need to stress too much about minor details when it's rendered for users.
I agree with you! As a junior dev, I always tried to keep things neatly formatted. It makes life easier in the long run, especially when you're debugging. But if your HTML is auto-generated, focusing too much on indentation might just be overkill!