Skip to content

Instantly share code, notes, and snippets.

@greatestview
Last active May 3, 2020 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greatestview/f6f3f28f80964da54c9bef482eb808c8 to your computer and use it in GitHub Desktop.
Save greatestview/f6f3f28f80964da54c9bef482eb808c8 to your computer and use it in GitHub Desktop.
Thoughts about how React components could be implemented in PHP
<script>
import React from 'react';
function Teaser(props) {
return (
<div class="teaser">
<h2>{props.title}</h2>
<p>{props.text}</p>
</div>
);
}
</script>
<script>
import React from 'react';
function Teaser(props) {
return React.createElement(
"div",
{class: "teaser"},
[
React.createElement(
"h2",
null,
props.title
),
React.createElement(
"p",
null,
props.text
),
]
);
}
</script>
<?php
function Teaser($props) {
$title = esc_html($props["title"]);
$text = esc_html($props["text"]);
$image_url = getImageUrlById($props["imageId"]);
return <<<HTML
<div class="teaser">
<h2>$title</h2>
<img src="$image_url" alt>
<p>$text</p>
</div>
HTML;
}
// ---
include "./Teaser.php";
echo Teaser([
"title" => "Ich bin der Teaser Title",
"text" => "Ich bin der Teaser Text",
"imageId" => 12
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment