Twig - это язык шаблонов, поэтому ваш HTML входит в ваши шаблоны.Как ваш test.html
.Затем в PHP вы получаете все данные, необходимые для вашего шаблона, и передаете его.
<?php
session_start();
require_once 'vendor/autoload.php';
require_once 'libs/user.php';
require_once 'config.php';
function getRecentForumTopics()
{
$communityUrl = 'https://domain.tld/';
$apiKey = '123';
$endpoint = '/forums/topics';
$vars = '?sortDir=desc&perPage=4';
$curl = curl_init($communityUrl.'api'.$endpoint.$vars);
curl_setopt_array(
$curl,
[
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => "{$apiKey}:",
]
);
$response = curl_exec($curl);
$values = json_decode($response, true);
$data = [];
foreach ($values['results'] as $value) {
$data[] = [
'title' => $value['title'],
'url' => $value['firstPost']['url'],
];
}
return $data;
}
$data = array(
"WebTitle" => "name",
//"User" => User::GetData($_SESSION['user_id'])
"recentFormTopics" => getRecentForumTopics() // <- This is where you are getting the data that is going to be available in twig
);
$loader = new Twig_Loader_Filesystem('templates/');
$twig = new Twig_Environment($loader, array(
'cache' => 'c_cache',
'debug' => 'false'
));
echo $twig->render('test.html', $data);
Так что теперь, когда вы передаете data
в test.html, обновите свой шаблон ветки, чтобы использовать его
{{ include('includes/head.html') }}
<body>
{{ include('includes/nav.html') }}
<section class="container mt-3">
<div class="row">
<div class="col-12">
<h2>Recent Topics</h2>
{# This is where you will use the data being passed in #}
<ol>
{% for recentFormTopic in recentFormTopics %}
<li>
<a href="{{ recentFormTopic.url }}" target="_blank">{{ recentFormTopic.title }}</a>
</li>
{% endfor %}
</ol>
</div>
</div>
</section>
{{ include('includes/footer.html') }}
</body>
</html>