2016-11-19 78 views
2

我试图打印输入字段'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垄断”。

我的问题,我该如何在Twig中执行以下PHP代码?

<?php 
echo "gameTitle is ".$_POST['gameTitle']; 
?> 

更新: - 我不使用Symfony的,只是嫩枝: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') }} 
+0

尝试'app.request.post( 'gameTitle')':你应该把它们传递给模板明确,例如:

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]); 

然后按如下方式使用它? – JOUM

+0

或'app.request.request.get('gameTitle')' – sensorario

回答

0

据我所知,香草树枝默认情况下不提供访问variables的请求。

{% if post.gameTitle is defined %} 
Game title: {{ post.gameTitle }} 
{% endif%} 
+0

我没有使用Symfony,只是Twig:http://twig.sensiolabs.org/ – Ducky

+0

@ user4975696,那么你应该明确地传递变量。我已经更新了答案 –

+0

是的,这很有用,谢谢! 我接受了你的回答。 – Ducky

1

您应该使用

{{ app.request.request.get("gameTitle") }} 

它已经变了。