-1

I would like to know how to access to a javascript variable inside another html.twig file in symfony2. Let's assume that we have two html\twig files: file1.html.twig and file2.html.twig, their codes are as below:

The code of file1.html.twig:

 <html>
    <head>
    <script>
    $(document).ready(function() {
      var calendar = $('#calendar').fullCalendar({
       //here is some code
      });
    });
    </script>
    </head>
    <body>
    </body>
    </html>

The code of file2.html.twig:

<html>
        <head>
        <script>
        function f1(){
            //here is the code that I need to access to the variable calendar of the file file1.html.twig
        }
        </script>
        </head>
        <body>
<form id="EventForm" action='{{path('ikproj_groupe_homepaeventsAdd',{id:idg})}}' method="POST" {{ form_enctype(form) }} onsubmit="f1();">
//here the body of the form
</form>
        </body>
        </html>

Actually, what I would like to know is how to access to the variable "calendar" of file1.html.twig inside the file file2.html.twig. So my question is it possible to do that??..If yes, how?

Nadim
  • 382
  • 1
  • 7
  • 29
  • Would it be a problem to pass that variable to next html file by GET query string? – HelpNeeder Sep 15 '14 at 15:19
  • you will make things easier if you merge them – john Smith Sep 15 '14 at 15:22
  • possible duplicate of [How to access to a javascript function inside another html.twig file in symfony2?](http://stackoverflow.com/questions/25683340/how-to-access-to-a-javascript-function-inside-another-html-twig-file-in-symfony2) – Rooneyl Sep 15 '14 at 15:30
  • @HelpNeeder: actually what I need is just to access to the variable "calendar" from (inside) "file2.html.twig", not to pass a variable from a file to another. Do you have any idea about how to do that? – Nadim Sep 15 '14 at 15:31
  • @johnSmith: what do you mean by "merge them"?...Can you give me more explanation please?..Thanks in advance. – Nadim Sep 15 '14 at 15:33
  • @Rooneyl: No, this is another topic. By the way, do you have any idea about how to access to a javascript variable from another html.twig file? – Nadim Sep 15 '14 at 15:35
  • @NadimAkram I think you are confusing matters by refering to twig templates, what you want is to pass a js object between requests. [Take a look at this question](http://stackoverflow.com/questions/7709289/how-to-pass-javascript-object-from-one-page-to-other) – Rooneyl Sep 15 '14 at 15:43
  • @Nadim Akram, I'm afraid I cannot help you with this. I never used 'twig' before. But sometimes, the simplest solution, is best solution. – HelpNeeder Sep 15 '14 at 15:57
  • @Rooneyl: I just need to access to a javascript variable from another html.twig file, not to pass a variable from a file to another!! – Nadim Sep 15 '14 at 15:59

1 Answers1

2

Put the code you want to share in a .js file:

// src/Acme/FooBundle/Resources/public/js/sharedCode.js

$(document).ready(function() {
    var calendar = $('#calendar').fullCalendar({
       //here is some code
    });
});

Then just include the script everywhere you want:

<html>
<head>
    ...
</head>
<body>
    ...

    {% javascripts '@AcmeFooBundle/Resources/public/js/sharedCode.js' %}
        <script type="text/javascript" src="{{ asset_url }}"></script>
    {% endjavascripts %}
</body>
</html>
albertedevigo
  • 18,262
  • 6
  • 52
  • 58
  • I am using Assetic here and not blocks, includes or inheritance stuff, but there are lots of ways to achieve this. – albertedevigo Sep 16 '14 at 09:18
  • Well, I did that, but Symfony displays this error message: An exception has been thrown during the compilation of a template ("You must add IkprojGroupeBundle to the assetic.bundle config to use the {% javascripts %} tag in IkprojGroupeBundle:GroupeEvents:Addeventgroupe.html.twig.") in "IkprojGroupeBundle:GroupeEvents:Addeventgroupe.html.twig".By the way, this is the code I added: `{% javascripts '@IkprojGroupeBundle/Resources/public/js/sharedCode.js' %} {% endjavascripts %}` – Nadim Sep 16 '14 at 11:35
  • 1
    Of course, you have to configure Assetic by adding the bundle in `config.yml`. Check this [configuration reference](http://symfony.com/doc/current/reference/configuration/assetic.html). If you are going to use Assetic, you should read the related docs. – albertedevigo Sep 17 '14 at 08:22