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

9
  • Would it be a problem to pass that variable to next html file by GET query string? Commented Sep 15, 2014 at 15:19
  • you will make things easier if you merge them Commented Sep 15, 2014 at 15:22
  • possible duplicate of How to access to a javascript function inside another html.twig file in symfony2? Commented Sep 15, 2014 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? Commented Sep 15, 2014 at 15:31
  • @johnSmith: what do you mean by "merge them"?...Can you give me more explanation please?..Thanks in advance. Commented Sep 15, 2014 at 15:33

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

I am using Assetic here and not blocks, includes or inheritance stuff, but there are lots of ways to achieve this.
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' %} <script type="text/javascript" src="{{asset('bundles/ikprojgroupe/js/sharedCode.js')}}"></script> {% endjavascripts %}
Of course, you have to configure Assetic by adding the bundle in config.yml. Check this configuration reference. If you are going to use Assetic, you should read the related docs.

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.