Created
March 28, 2018 12:50
-
-
Save iampava/6e7d3698f5c15d756bdd429b623c0a38 to your computer and use it in GitHub Desktop.
Reusable HTML "components" with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function createHeader() { | |
return ' | |
<header> | |
<h1> | |
<span class="emoji">π</span> | |
TW Checklist | |
<span class="emoji">π</span> | |
</h1> | |
</header> | |
'; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head></head> | |
<body> | |
<?php | |
include "./header.component.php"; | |
echo createHeader(); | |
?> | |
</body> | |
</html> |
also works
<?php function createHeader() { ?>
<header>
<h1>
<span class="emoji">π</span>
TW Checklist
<span class="emoji">π</span>
</h1>
</header>
<? } ?>
Also works (no PHP):
<header>
<h1>
<span class="emoji">π</span>
TW Checklist
<span class="emoji">π</span>
</h1>
</header>
Also works (no PHP):
<header> <h1> <span class="emoji">π</span> TW Checklist <span class="emoji">π</span> </h1> </header>
Yes but it is better to wrap it in a function, Its importance appears when you need to pass props.
When dealing with dynamic values, it is essential to ensure that this data is properly escaped to avoid security issues such as HTML or JavaScript injection. A recommended practice is to use the htmlspecialchars
function, as shown in the example below:
<?php
echo '<input type="hidden" value="' . htmlspecialchars($data) . '" />'."\n";
?>
This approach is described in the official PHP documentation: FAQ: htmlspecialchars
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool. That's what I've been looking for