feat: ✨ added first draft of Database based website rendering
Some checks failed
/ phplint (push) Failing after 3s
Some checks failed
/ phplint (push) Failing after 3s
This commit is contained in:
parent
5bcba0b689
commit
fcc42bde20
4 changed files with 171 additions and 23 deletions
|
@ -5,18 +5,59 @@ require_once 'vendor/autoload.php';
|
|||
|
||||
$sql = mysqli_connect('mysql', 'root', 'example', 'dragon_fire');
|
||||
|
||||
function process_post_data($post_data) {
|
||||
if($post_data == null) {
|
||||
$post_data = [
|
||||
"found" => false
|
||||
];
|
||||
}
|
||||
else {
|
||||
$post_data["found"] = true;
|
||||
$post_data["post_metadata"] = json_decode($post_data["post_metadata"]);
|
||||
}
|
||||
|
||||
return $post_data;
|
||||
}
|
||||
|
||||
function get_post_by_path($path) {
|
||||
global $sql;
|
||||
|
||||
$path = chop($path, '/');
|
||||
|
||||
$qry = "SELECT * FROM posts WHERE post_path = ?";
|
||||
|
||||
$stmt = $sql->prepare($qry);
|
||||
$stmt->bind_param("s", $path);
|
||||
$stmt->execute();
|
||||
|
||||
// $result = $stmt->get_result();
|
||||
$post_data = $stmt->get_result()->fetch_assoc();
|
||||
|
||||
return $stmt->get_result()->fetch_assoc();
|
||||
return process_post_data($post_data);
|
||||
}
|
||||
|
||||
function get_subposts($path) {
|
||||
global $sql;
|
||||
|
||||
$path = chop($path, '/');
|
||||
|
||||
$path_depth = substr_count($path, "/");
|
||||
|
||||
$qry = "SELECT post_path, post_metadata
|
||||
FROM posts
|
||||
WHERE (post_path LIKE CONCAT(?,'/%'))
|
||||
AND post_path_depth = ?
|
||||
ORDER BY post_create_time DESC
|
||||
LIMIT 10";
|
||||
|
||||
$stmt = $sql->prepare($qry);
|
||||
$stmt->bind_param("si", $path, $path_depth);
|
||||
$stmt->execute();
|
||||
|
||||
$post_data = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
|
||||
|
||||
$post_data = array_map('process_post_data', $post_data);
|
||||
|
||||
return $post_data;
|
||||
}
|
||||
|
||||
//if (!$sql)
|
||||
|
@ -51,6 +92,19 @@ if($_SERVER['REQUEST_URI'] == '/') {
|
|||
header('Content-Type: application/json');
|
||||
echo json_encode(get_post_by_path($match[1]));
|
||||
|
||||
} elseif(preg_match('/^\/api\/subposts(.*)$/', $_SERVER['REQUEST_URI'], $match)) {
|
||||
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(get_subposts($match[1]));
|
||||
|
||||
} elseif(true) {
|
||||
$post = get_post_by_path($_SERVER['REQUEST_URI']);
|
||||
|
||||
echo $twig->render('about.html', [
|
||||
"post" => $post,
|
||||
"subposts" => get_subposts($_SERVER['REQUEST_URI'])
|
||||
]);
|
||||
|
||||
} elseif(preg_match('/^\/about(.html)?$/', $_SERVER['REQUEST_URI'])) {
|
||||
echo $twig->render('about.html');
|
||||
} elseif(preg_match('/^\/gallery\/([^\?]+)/', $_SERVER['REQUEST_URI'])) {
|
||||
|
|
|
@ -3,11 +3,23 @@
|
|||
{% extends "root.html" %}
|
||||
|
||||
{% block second_title %}
|
||||
<h2> Abouts </h2>
|
||||
<h2> {{ post.post_metadata.title }} </h2>
|
||||
{% endblock %}
|
||||
|
||||
{%block main_content%}
|
||||
<article>
|
||||
{{ include('about.md')|markdown_to_html }}
|
||||
{{ post['post_content']|markdown_to_html }}
|
||||
</article>
|
||||
|
||||
{% if subposts|length > 0 %}
|
||||
<article>
|
||||
<h3>Related:</h3>
|
||||
<ul>
|
||||
{% for subpost in subposts %}
|
||||
<li><a href={{subpost.post_path}}>{{ subpost.post_metadata.title }}</a></li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</article>
|
||||
{%endif%}
|
||||
|
||||
{%endblock%}
|
||||
|
|
19
www/templates/generic_markdown_post.html
Normal file
19
www/templates/generic_markdown_post.html
Normal file
|
@ -0,0 +1,19 @@
|
|||
|
||||
{% extends "root.html" %}
|
||||
|
||||
{% block second_title %}
|
||||
<h2> Abouts </h2>
|
||||
{% endblock %}
|
||||
|
||||
{%block main_content%}
|
||||
<article>
|
||||
{{ include('about.md')|markdown_to_html }}
|
||||
</article>
|
||||
|
||||
<ul>
|
||||
{% for subpost in subposts %}
|
||||
<li> {{ subpost.title }} </li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
||||
{%endblock%}
|
Loading…
Add table
Add a link
Reference in a new issue