Last active
February 27, 2019 13:51
-
-
Save kobus1998/c4f73882b32c95057a1e83b3a4d0515e to your computer and use it in GitHub Desktop.
generate html
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 arrayToAttrs($attrs) | |
{ | |
if (!is_array($attrs)) { | |
return ''; | |
} | |
$result = ''; | |
foreach($attrs as $key => $val) | |
{ | |
$result .= "{$key}=\"{$val}\""; | |
} | |
return $result; | |
} | |
function node($name, $content = null, $attrs = []) | |
{ | |
$node = "<$name "; | |
$node .= arrayToAttrs($attrs); | |
if ($content == null) { | |
$node .= "/>"; | |
return $node; | |
} | |
print_r($attrs); | |
$node .= ">$content</$name>"; | |
return $node; | |
} | |
echo node('div', | |
implode([node('h1', 'login'), | |
node('input', null, ['name' => 'email']), | |
node('input', null, ['name' => 'password']), | |
node('button', 'submit') | |
]) | |
, ['class' => 'login']); | |
/* | |
<div class="login"> | |
<h1>login</h1> | |
<input name="email" /> | |
<input name="password" /> | |
<button>submit</button> | |
</div> | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment