Skip to content

Commit 2879f11

Browse files
committed
Added standard element set, with className and style support
1 parent 97efb1a commit 2879f11

File tree

1 file changed

+195
-2
lines changed

1 file changed

+195
-2
lines changed

src/functions.php

Lines changed: 195 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ function parse($nodes) {
260260

261261
$props["children"] = $children;
262262

263-
if (function_exists("pre_" . $node["name"])) {
264-
$code .= "pre_" . $node["name"] . "([" . PHP_EOL;
263+
if (function_exists("Pre\\Phpx\\Renderer\\_" . $node["name"])) {
264+
$code .= "Pre\\Phpx\\Renderer\\_" . $node["name"] . "([" . PHP_EOL;
265265
}
266266

267267
else {
@@ -294,3 +294,196 @@ function parse($nodes) {
294294
function compile($code) {
295295
return parse(nodes(tokens($code)));
296296
}
297+
298+
namespace Pre\Phpx\Renderer;
299+
300+
use Closure;
301+
use Pre\Collections\Collection;
302+
303+
function element($name, $props, callable $attrs = null) {
304+
$code = "<{$name}";
305+
306+
$className = $props["className"] ?? null;
307+
308+
if (is_callable($className)) {
309+
$className = $className();
310+
}
311+
312+
if ($className instanceof Collection) {
313+
$className = $className->toArray();
314+
}
315+
316+
if (is_array($className)) {
317+
$combined = "";
318+
319+
foreach ($className as $key => $value) {
320+
if (is_string($key)) {
321+
$combined .= !!$value ? " {$key}" : "";
322+
}
323+
324+
else {
325+
$combined .= " {$value}";
326+
}
327+
}
328+
329+
$className = trim($combined);
330+
}
331+
332+
if ($className) {
333+
$code .= " class='{$className}'";
334+
}
335+
336+
$style = $props["style"] ?? null;
337+
338+
if (is_callable($style)) {
339+
$style = $style();
340+
}
341+
342+
if ($style instanceof Collection) {
343+
$style = $style->toArray();
344+
}
345+
346+
if (is_array($style)) {
347+
$styles = [];
348+
349+
foreach ($style as $key => $value) {
350+
$styles[] = "{$key}: {$value}";
351+
}
352+
353+
$style = join("; ", $styles);
354+
}
355+
356+
if ($style) {
357+
$code .= " style='{$style}'";
358+
}
359+
360+
$code .= ">";
361+
362+
foreach ($props["children"] as $child) {
363+
$code .= $child;
364+
}
365+
366+
$code .= "</{$name}>";
367+
368+
return $code;
369+
}
370+
371+
372+
define("ELEMENTS", [
373+
"a",
374+
"abbr",
375+
"address",
376+
"area",
377+
"article",
378+
"aside",
379+
"audio",
380+
"b",
381+
"base",
382+
"bdi",
383+
"bdo",
384+
"blockquote",
385+
"body",
386+
"br",
387+
"button",
388+
"canvas",
389+
"caption",
390+
"cite",
391+
"code",
392+
"col",
393+
"colgroup",
394+
"data",
395+
"datalist",
396+
"dd",
397+
"del",
398+
"details",
399+
"dfn",
400+
"div",
401+
"dl",
402+
"dt",
403+
"em",
404+
"embed",
405+
"fieldset",
406+
"figcaption",
407+
"figure",
408+
"footer",
409+
"form",
410+
"h1",
411+
"h2",
412+
"h3",
413+
"h4",
414+
"h5",
415+
"h6",
416+
"head",
417+
"header",
418+
"hr",
419+
"html",
420+
"i",
421+
"iframe",
422+
"img",
423+
"input",
424+
"ins",
425+
"kbd",
426+
"label",
427+
"legend",
428+
"li",
429+
"link",
430+
"main",
431+
"map",
432+
"mark",
433+
"meta",
434+
"meter",
435+
"nav",
436+
"noframes",
437+
"noscript",
438+
"object",
439+
"ol",
440+
"optgroup",
441+
"option",
442+
"output",
443+
"p",
444+
"param",
445+
"pre",
446+
"progress",
447+
"q",
448+
"rp",
449+
"rt",
450+
"rtc",
451+
"ruby",
452+
"s",
453+
"samp",
454+
"script",
455+
"section",
456+
"select",
457+
"slot",
458+
"small",
459+
"source",
460+
"span",
461+
"strong",
462+
"style",
463+
"sub",
464+
"summary",
465+
"sup",
466+
"table",
467+
"tbody",
468+
"td",
469+
"template",
470+
"textarea",
471+
"tfoot",
472+
"th",
473+
"thead",
474+
"time",
475+
"title",
476+
"tr",
477+
"track",
478+
"u",
479+
"ul",
480+
"var",
481+
"video",
482+
"wbr",
483+
]);
484+
485+
foreach (ELEMENTS as $element) {
486+
eval("namespace Pre\Phpx\Renderer { function _{$element}(\$props) {
487+
return element('{$element}', \$props);
488+
} }");
489+
}

0 commit comments

Comments
 (0)