<p>As Twig is rendered server side before passed to a browser for JS execution I found out that it can't be done except <a href="https://github.com/FriendsOfSymfony/FOSJsRoutingBundle" rel="nofollow noreferrer">FOSsRoutingBundle</a> which allows you to expose your routing in your JavaScript code. I don't know if that would be a great idea.</p>

<p>Anyway. I found workaround solution that seems to be working for me.</p>

<p><strong>Here is the Ajax post action with simple window redirection.</strong></p>

<pre><code>// Send it to the server
$.post('/', data, function(response, textStatus, jqXHR) {

  if (response.success) {

    // Request for entry ID key string to be used in HTTP redirect link
    var entryId = response.id

    // HTTP redirect
    window.location.replace("{{ siteUrl }}" + entryId + "/someUri");

    } else {
    // response.error
  }

});
</code></pre>

<p>Don't forget Craft's routing!</p>

CC BY-SA 3.0
2017-04-16T14:08:15.410Z
2017-04-16T18:34:12.397Z
2017-04-16T18:34:12.397Z
Dominik Krulak
<p>I've came to the situation where I need to use JS variable string and use it in Twig's variable. </p>

<p>The plan is built on the <a href="https://github.com/pixelandtonic/GuestEntries" rel="nofollow noreferrer">Guest entries</a> and Ajax form submission. On successful Ajax form submission I'm calling for entry's keys (especially <code>id</code> key) and using these keys to set a cookie names. Those cookie names should be used on next page to get into the entry that just got submitted. Can it be achieved?</p>

<javascript><guest-entries>
Can a JS string variable be used in Twig?
# Can a JS string variable be used in Twig?

(Asked by: username_0 on 2017-04-16)

I've came to the situation where I need to use JS variable string and use it in Twig's variable.

The plan is built on the [Guest entries](https://github.com/pixelandtonic/GuestEntries) and Ajax form submission. On successful Ajax form submission I'm calling for entry's keys (especially `id` key) and using these keys to set a cookie names. Those cookie names should be used on next page to get into the entry that just got submitted. Can it be achieved?

**Tags:** javascript, guest-entries

---

## 1 answers

### Answer by username_0 on 2017-04-16. Score: 1 | Accepted answer

As Twig is rendered server side before passed to a browser for JS execution I found out that it can't be done except FOSsRoutingBundle which allows you to expose your routing in your JavaScript code. I don't know if that would be a great idea.
Anyway. I found workaround solution that seems to be working for me.
Here is the Ajax post action with simple window redirection.
// Send it to the server
$.post('/', data, function(response, textStatus, jqXHR) {

  if (response.success) {

    // Request for entry ID key string to be used in HTTP redirect link
    var entryId = response.id

    // HTTP redirect
    window.location.replace("{{ siteUrl }}" + entryId + "/someUri");

    } else {
    // response.error
  }

});

Don't forget Craft's routing!


---

