1

I've got some symfony template files with <script> chunks, obviously this needs to be in the head.

these chunks of javascript make use of helpers like url_for and also echo out symfony template variables.

I need to be able to load this javascript using include_javascript or similar but what is the best way to still use the symfony templating functions and access the variables I need to generate the script while loading it into the head not the body of the page.

1 Answer 1

2

In this case I use a particular Symfony module, that fills JavaScript variables with PHP code.

To do so, you can create a module called "javascript" for example, which has a single action in it. The template associated to this action must be have the following name : ...Success.js.php.

In this template, you can fill your JavaScript variables like this :

    var global = {
        misc: {
            userCulture : "<?php echo $sf_user->getCulture() ?>",
            serverName : "<?php echo $_SERVER["SERVER_NAME"]?>",
        },
    ...
    }

Then, in your *appName*Configuration.class.php (located in the config folder of your app), you must add the following code :

    class indexConfiguration extends sfApplicationConfiguration
    {
        public function configure()
        {
          $this->dispatcher->connect('context.load_factories', array($this, 'listenToContextLoadFactoriesEvent'));
        }

        public function listenToContextLoadFactoriesEvent(sfEvent $event)
        {
                $event->getSubject()->getResponse()->addJavascript($event->getSubject()->getRouting()->generate('javascript_variables'));
        }
    }

Then, you must add the following route to your routing.yml file :

    javascript_variables:
      url:    /*moduleName*/*actionName*.:sf_format
      param:  { module: *moduleName*, action: *actionName*, sf_format: js }

Now, you're able to access these JavaScript variables in any of your js files, i.e. :

    $(document).ready(function() {
            alert(global.misc.userCulture);
    });
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks very much for your answer, I would never have known this approach. One issue is that I don't need access to the same variables for every template so this 'global' approach seems like it isn't quite a full fit , the variables I'm using are much more module/action specific and what variiables I am using change by module and action (for example - a url for ajax submission changes across module actions) could this approach be adapted to serve this need?
In the template, you can create the JavaScript variables you'll need according to the current module. For example, $module = $sf_context->getModuleName(); switch ($module) { case "admin": var global = “... “}.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.