I am new to Craft and Twig in general. I have encountered the following problems and struggling to find a solution for hours.

  1. I want to access the value of "foo" input field inside the twig delimiters to set the "json" variable.
  2. I want to change the "clicked" boolean, whenever the submit button is clicked.

The myFunc is a function that I am using from a custom plugin so it is not relative. I have commented what I've last tried and does not work for me.

I am using Craft 3, if that helps.

{% block body %}
    {% set clicked = false %}

    <form name="search" action="{% set clicked = true %}">
        <label for="foo">Foo</label>
        <input name="foo" type="text"/>
        <input type="submit" value="Get Json"/>
        {#    <a href="{{ set clicked = true }}"#}

    {% if clicked %}
        {% set json = jsonator_myFunc(search.foo.vars.value) %}
    {% endif %}

{% endblock %}


A few more days into Craft CMS and chill and I realize how silly my question is.

Thank you for the replies. The solution which I was looking for is:

  • 2
    For clarity, Twig is a wrapper for PHP, a server-side language. Twig/PHP gets parsed down to regular HTML before it gets sent to the browser. Essentially, you'd interact with Twig in the exact same way as you'd interact with PHP.
    – Lindsey D
    Jul 29, 2019 at 15:15

2 Answers 2


You can do it without JavaScript (if you don't mind the page refreshing) by accessing the request variable:


{{ craft.app.request.getParam('foo') }} {# Should be the value of the input named 'foo' #}
  • This is exactly what I ended up using. Thanks anyways! The only thing that bothers me is that I only managed to get it work with a form with a "get" method and the parameters are visible in the address bar. Do you know a way to hide them?
    – ves
    Jul 29, 2019 at 15:04
  • 1
    You can set the form's method to 'post' and the server should still be able to access the request variables, without showing them in the address bar. Jul 29, 2019 at 18:38

Twig (at least within the context of Craft) doesn't work this way I'm afraid. The Twig template is processed on the server and HTML is returned to the browser.

You're trying to change the Twig based on an event in the Browser, but that at that time, there is no longer Twig, just HTML.

So your solution will need to involve Javascript to change the HTML/DOM rather than the approach you're going for here.

  • Thanks! I'am solving it right now with js but I was hoping there will be a "simpler" solution.
    – ves
    Jul 29, 2019 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.