Search code examples
twigtimber

Setting echo $_GET inside of a form


I have the following set up below.

My issue is I'm not sure how to convert an inline echo $_GET. Is it just the same as {{ 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>

Solution

  • Thanks to @DarkBee for pointing the above out: Passing PHP global variables via Twig/Timber

    To access $_POST variables, use this:

    {{ request.post.name_of_var }}
    

    To access $_GET variables, use this:

    {{ request.get.name_of_var }}
    

    So that would make this:

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

    Be this:

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