【问题标题】:Passing data to Slim Renderer template PHP将数据传递给 Slim Renderer 模板 PHP
【发布时间】:2018-09-18 21:28:55
【问题描述】:

我正在使用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 的新手。

【问题讨论】:

  • 您确定$args['playerid'] 已设置吗?如果在渲染dataNotFound.phtml之前加上var_dump($args),输出是什么?
  • 这一行永远不会打印一些东西:&lt;?php if(isset($playerid)) htmlspecialchars($playerid) ?&gt; try &lt;?php if(isset($playerid)) echo htmlspecialchars($playerid); ?&gt;
  • 倾倒$args 之前 of return $this-&gt;renderer-&gt;render($response, 'user.phtml', $args); 向我展示了我预期的数据。
  • 谢谢@DanielO,解决了。

标签: php templates render slim-3


【解决方案1】:

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">
...

【讨论】:

    【解决方案2】:

    你可以做类似的事情。

    • 路由器文件

      $this->renderer->args = array( 'pid' => $args['pid'], );
      return $this->renderer->render($response, "user.phtml");

    • ./templates/dataNotFound.phtml

    echo $this->args['pid'];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多