我正在使用Slim Framework v3,我在将数据从PHP传递到Slim模板时遇到问题。
例如:
$app->get('/user/{playerid}', function (Request $request, Response $response, array $args) {
$playerid = $args['playerid'];
$this->logger->info("Someone requested to see the score of the following player: [".$playerid."]");
$db = Database::Instance();
$row = $db->getPlayer($playerid);
if ($row === false)
return $this->renderer->render($response, 'dataNotFound.phtml', $args); // Not working: $playerid no displayed on HTML
// Sets the player info on the template (this works)
$args['name'] = $row['Name'];
$args['language'] = $row['Language'];
$args['score'] = $row['Score'];
$args['card'] = $row['CardPngBase64'];
return $this->renderer->render($response, 'user.phtml', $args);
});
工作模板(./templates/user.phtml
)是:
<div class="container" style="margin-top: 4%">
<?php
if (isset($playerid) && isset($name) && isset($language) && isset($score) && isset($card)) : ?>
<h1 class="jumbotron"><img align="center" src="<?php echo htmlspecialchars($card); ?>" /><br> <?= htmlspecialchars($name); ?>'s scores </h1>
<div class="row text-center">
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Country</th>
<th scope="col">Score</th>
</tr>
</thead>
<tbody>
<tr>
<td><?= htmlspecialchars($name); ?></td>
<td><?= htmlspecialchars(Locale::getDisplayRegion('-'.$language, 'en')); ?></td>
<td><?= htmlspecialchars($score); ?></td>
</tr>
</tbody>
</table>
</div>
<?php else: ?>
<h1 style="color: darkred">⛔️ Internal error: can't get the players data.</h1>
<?php endif; ?>
<div class="row" style="position: relative; margin-top: 50%">
<a class="text-justify" href="http://responseable.csp.it">Go back to the main page</a>
</div>
</div>
不工作模板(./templates/dataNotFound.phtml
)是:
<div class="container" style="margin-top: 4%">
<h1 class="jumbotron">♂️ Player not found ♀️</h1>
<hr style="margin-top: 5%; margin-bottom: 5%">
<div class="row">
<p>‼️</p>
<br>
<p>The player <?php if(isset($playerid)) htmlspecialchars($playerid) ?> does not exists, please retry.</p>
</div>
<div class="row" style="position: relative; margin-top: 50%">
<a class="text-justify" href="http://responseable.csp.it">Go back to the main page</a>
</div>
</div>
因此,当数据库找到$playerid
时,它会返回正确的渲染模板(user.phtml)并正确显示播放器数据。
但是当数据库找不到ID时,dataNotFound.phtml的呈现不起作用,特别是,我可以正确地看到所有HTML页面,但htmlspecialchars($playerid)
缺失或为空。
这可能是一个小错误,但我找不到它。有人能帮我吗?但是,我是Slim的新人。
答案 0 :(得分:0)
您可以做类似的事情。
路由器文件
$ this-> renderer-> args = array(
'pid'=> $ args ['pid'],);
返回$ this-> renderer-> render($ response,“ user.phtml”);
./ templates / dataNotFound.phtml
echo $ this-> args ['pid'];
答案 1 :(得分:0)
routes.php
$app->get('/panel',function(Request $request, Response $response, array $args) use ($container){
return $container->get('renderer')->render($response, 'panel.phtml', [
'key' => "val"
]);
});
panel.phtml
<?php print_r($data); ?>
<!doctype html>
<html lang="en">
...