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 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?

1 Answer 1


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!

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.