在表单内设置echo $ _GET

问题描述

我在下面进行了以下设置。

我的问题是我不确定如何转换内联echo $_GET。它和{{ fn( echo $_GET['units'] === 'Miles' ? 'selected' : null ) }}一样吗?

<form method="get" action="{{ post.link }}">
    <div>
        <span>Find a Store within</span>
        <input name="proximity" type="number" placeholder="15" value="<?PHP echo $_GET['proximity'] ?>" />
            <select name="units">
                <option value="Miles" <?PHP echo $_GET['units'] === 'Miles' ? 'selected' : null; ?>>Miles</option>
                <option value="K" <?PHP echo $_GET['units'] === 'K' ? 'selected' : null; ?>>Km</option>
            </select>
            <span>from</span>
        <input name="origin" type="text" placeholder="Your Address" value="<?PHP echo $_GET['origin'] ?>" />
    </div>
    <div>
        <input type="submit" value="Search" />
        <a href="{{ post.link }}">Reset</a>
    </div>
</form>

解决方法

感谢@DarkBee指出以上几点:Passing PHP global variables via Twig/Timber

要访问$ _POST变量,请使用以下命令:

{{ request.post.name_of_var }}

要访问$ _GET变量,请使用以下命令:

{{ request.get.name_of_var }}

因此可以做到这一点:

<input name="origin" type="text" placeholder="Your Address" value="<?php echo $_GET['origin'] ?>" />

是这样:

<input name="origin" type="text" placeholder="Your Address" value="{{ request.get.origin}}