gpt4 book ai didi

php - 如何以 Twig 形式访问 $_POST?

转载 作者:行者123 更新时间:2023-12-03 20:47:05 24 4
gpt4 key购买 nike

我正在尝试在 Twig 中打印输入字段“gameTitle”的值。

这是我的代码:

<h1>New game</h1>
<form method="post" action="">
<label>Game Title</label>
<input type="text" value="Monopoly" name="gameTitle"><br>
<input class="btn btn-success" name="submit" type="submit" value="Add game">
</form>
{% if app.request.post('submit') %}
{{ app.request('gameTitle')}}
{% endif %}

我也试过:
{{ app.request.parameter.post('gameTitle}

结果我想打印这个结果:“gameTitle is Monopoly”。

我的问题 ,我如何在 Twig 中执行以下 PHP 代码?
<?php
echo "gameTitle is ".$_POST['gameTitle'];
?>

更新:
- 我没有使用 Symfony,只是使用 Twig: http://twig.sensiolabs.org/
这对我不起作用:
{{app.request.post('gameTitle')}}
{{app.request.request.get('gameTitle')}}
{{ app.request.request.get("gameTitle") }}
gameTitle is {{ app.request.request.post('gameTitle') }}

最佳答案

据我所知,默认情况下,vanilla Twig 不提供对请求 variables 的访问。您应该将它们显式传递给模板,例如:

require __DIR__ . '/vendor/autoload.php';

$loader = new Twig_Loader_Filesystem(__DIR__ . '/templates');
$twig = new Twig_Environment($loader, array(
'cache' => __DIR__ . '/tpl_cache',
));

echo $twig->render('template.twig', ['post' => $_POST]);

然后按如下方式使用它:
{% if post.gameTitle is defined %}
Game title: {{ post.gameTitle }}
{% endif%}

关于php - 如何以 Twig 形式访问 $_POST?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40691048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com