Pass variables from controller to javascript function in Fat-Free Framework











up vote
1
down vote

favorite












I have some values on my controller that I want to pass to a javascript function in the view page.



In the controller, I have:



$f3->set('value', $value);


I can access the value on the view with {{@value}}, but how do I use(access) that value inside a javascript function on the view page??



<script type="text/javascript">
var value = XXX; //XXX in the {{@value}}, how do i access it in here???
</script>









share|improve this question


























    up vote
    1
    down vote

    favorite












    I have some values on my controller that I want to pass to a javascript function in the view page.



    In the controller, I have:



    $f3->set('value', $value);


    I can access the value on the view with {{@value}}, but how do I use(access) that value inside a javascript function on the view page??



    <script type="text/javascript">
    var value = XXX; //XXX in the {{@value}}, how do i access it in here???
    </script>









    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have some values on my controller that I want to pass to a javascript function in the view page.



      In the controller, I have:



      $f3->set('value', $value);


      I can access the value on the view with {{@value}}, but how do I use(access) that value inside a javascript function on the view page??



      <script type="text/javascript">
      var value = XXX; //XXX in the {{@value}}, how do i access it in here???
      </script>









      share|improve this question













      I have some values on my controller that I want to pass to a javascript function in the view page.



      In the controller, I have:



      $f3->set('value', $value);


      I can access the value on the view with {{@value}}, but how do I use(access) that value inside a javascript function on the view page??



      <script type="text/javascript">
      var value = XXX; //XXX in the {{@value}}, how do i access it in here???
      </script>






      javascript fat-free-framework






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 14:11









      Desmond Makhubele

      317




      317
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          It depends on the content stored inside $value.



          If it's a basic string with no single/double quote inside, then the following code will work:



          <script>
          var value='{{ @value }}';
          </script>


          If it's an integer, the following code will work:



          <script>
          var value={{ @value }};
          </script>


          ... although your IDE will probably report a syntax error.



          If it's a float, the following code will work:



          <script>
          var value={{ str_replace(',', '.', @value) }};
          </script>


          ... and your IDE will also probably report a syntax error. NB: the str_replace is for non-English locales that have decimal separator set to comma.



          For all the rest (strings including quotes or arrays), you should convert your data to JSON, using one of the following techniques:



          Technique 1:



          Convert data to JSON and dump it to a JS object.





          // controller.php (JSON encode)
          $f3->set('data',json_encode($data));




          <!-- template.html -->
          <script>
          var data={{ @data | raw }};
          </script>


          Pros: easy to use.



          Cons: your IDE will report a syntax error + extra call to raw.



          Technique 2:



          Convert data to JSON, dump it to a JS string and parse it.





          // controller.php (JSON encode + escape double quotes)
          $f3->set('data',str_replace('\u0022','\\u0022',
          json_encode($data,JSON_HEX_APOS|JSON_HEX_QUOT)));




          <!-- template.html -->
          <script>
          var data=JSON.parse('{{ @data | raw }}');
          </script>


          Cons: less easy to use + extra call to raw.



          Pros: your IDE will not report any syntax error.



          Technique 2bis:



          Embed technique 2 in a F3 template filter.





          // index.php
          $tpl=Template::instance();
          $tpl->filter('safejson',function($data){
          $raw=View::instance()->raw($data);
          return str_replace('\u0022','\\u0022',
          json_encode($raw,JSON_HEX_APOS|JSON_HEX_QUOT));
          });




          <!-- template.html -->
          <script>
          var data=JSON.parse('{{ @data | safejson }}');
          </script>


          Pros: easy to use + your IDE will not report any syntax error.



          Cons: extra call to raw.



          Technique 3:



          Convert data to JSON and embed it inside a DOM data- attribute.





          // controller.php (JSON encode)
          $f3->set('data',json_encode($data));




          <!-- template.html -->
          <div id="foo" data-json="{{ @data }}"></div>
          <script>
          var data=JSON.parse(document.getElementById('foo').dataset.json);
          </script>


          Pros: easy to use + your IDE will not report any syntax error + no extra call to raw.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249563%2fpass-variables-from-controller-to-javascript-function-in-fat-free-framework%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            It depends on the content stored inside $value.



            If it's a basic string with no single/double quote inside, then the following code will work:



            <script>
            var value='{{ @value }}';
            </script>


            If it's an integer, the following code will work:



            <script>
            var value={{ @value }};
            </script>


            ... although your IDE will probably report a syntax error.



            If it's a float, the following code will work:



            <script>
            var value={{ str_replace(',', '.', @value) }};
            </script>


            ... and your IDE will also probably report a syntax error. NB: the str_replace is for non-English locales that have decimal separator set to comma.



            For all the rest (strings including quotes or arrays), you should convert your data to JSON, using one of the following techniques:



            Technique 1:



            Convert data to JSON and dump it to a JS object.





            // controller.php (JSON encode)
            $f3->set('data',json_encode($data));




            <!-- template.html -->
            <script>
            var data={{ @data | raw }};
            </script>


            Pros: easy to use.



            Cons: your IDE will report a syntax error + extra call to raw.



            Technique 2:



            Convert data to JSON, dump it to a JS string and parse it.





            // controller.php (JSON encode + escape double quotes)
            $f3->set('data',str_replace('\u0022','\\u0022',
            json_encode($data,JSON_HEX_APOS|JSON_HEX_QUOT)));




            <!-- template.html -->
            <script>
            var data=JSON.parse('{{ @data | raw }}');
            </script>


            Cons: less easy to use + extra call to raw.



            Pros: your IDE will not report any syntax error.



            Technique 2bis:



            Embed technique 2 in a F3 template filter.





            // index.php
            $tpl=Template::instance();
            $tpl->filter('safejson',function($data){
            $raw=View::instance()->raw($data);
            return str_replace('\u0022','\\u0022',
            json_encode($raw,JSON_HEX_APOS|JSON_HEX_QUOT));
            });




            <!-- template.html -->
            <script>
            var data=JSON.parse('{{ @data | safejson }}');
            </script>


            Pros: easy to use + your IDE will not report any syntax error.



            Cons: extra call to raw.



            Technique 3:



            Convert data to JSON and embed it inside a DOM data- attribute.





            // controller.php (JSON encode)
            $f3->set('data',json_encode($data));




            <!-- template.html -->
            <div id="foo" data-json="{{ @data }}"></div>
            <script>
            var data=JSON.parse(document.getElementById('foo').dataset.json);
            </script>


            Pros: easy to use + your IDE will not report any syntax error + no extra call to raw.






            share|improve this answer



























              up vote
              2
              down vote



              accepted










              It depends on the content stored inside $value.



              If it's a basic string with no single/double quote inside, then the following code will work:



              <script>
              var value='{{ @value }}';
              </script>


              If it's an integer, the following code will work:



              <script>
              var value={{ @value }};
              </script>


              ... although your IDE will probably report a syntax error.



              If it's a float, the following code will work:



              <script>
              var value={{ str_replace(',', '.', @value) }};
              </script>


              ... and your IDE will also probably report a syntax error. NB: the str_replace is for non-English locales that have decimal separator set to comma.



              For all the rest (strings including quotes or arrays), you should convert your data to JSON, using one of the following techniques:



              Technique 1:



              Convert data to JSON and dump it to a JS object.





              // controller.php (JSON encode)
              $f3->set('data',json_encode($data));




              <!-- template.html -->
              <script>
              var data={{ @data | raw }};
              </script>


              Pros: easy to use.



              Cons: your IDE will report a syntax error + extra call to raw.



              Technique 2:



              Convert data to JSON, dump it to a JS string and parse it.





              // controller.php (JSON encode + escape double quotes)
              $f3->set('data',str_replace('\u0022','\\u0022',
              json_encode($data,JSON_HEX_APOS|JSON_HEX_QUOT)));




              <!-- template.html -->
              <script>
              var data=JSON.parse('{{ @data | raw }}');
              </script>


              Cons: less easy to use + extra call to raw.



              Pros: your IDE will not report any syntax error.



              Technique 2bis:



              Embed technique 2 in a F3 template filter.





              // index.php
              $tpl=Template::instance();
              $tpl->filter('safejson',function($data){
              $raw=View::instance()->raw($data);
              return str_replace('\u0022','\\u0022',
              json_encode($raw,JSON_HEX_APOS|JSON_HEX_QUOT));
              });




              <!-- template.html -->
              <script>
              var data=JSON.parse('{{ @data | safejson }}');
              </script>


              Pros: easy to use + your IDE will not report any syntax error.



              Cons: extra call to raw.



              Technique 3:



              Convert data to JSON and embed it inside a DOM data- attribute.





              // controller.php (JSON encode)
              $f3->set('data',json_encode($data));




              <!-- template.html -->
              <div id="foo" data-json="{{ @data }}"></div>
              <script>
              var data=JSON.parse(document.getElementById('foo').dataset.json);
              </script>


              Pros: easy to use + your IDE will not report any syntax error + no extra call to raw.






              share|improve this answer

























                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                It depends on the content stored inside $value.



                If it's a basic string with no single/double quote inside, then the following code will work:



                <script>
                var value='{{ @value }}';
                </script>


                If it's an integer, the following code will work:



                <script>
                var value={{ @value }};
                </script>


                ... although your IDE will probably report a syntax error.



                If it's a float, the following code will work:



                <script>
                var value={{ str_replace(',', '.', @value) }};
                </script>


                ... and your IDE will also probably report a syntax error. NB: the str_replace is for non-English locales that have decimal separator set to comma.



                For all the rest (strings including quotes or arrays), you should convert your data to JSON, using one of the following techniques:



                Technique 1:



                Convert data to JSON and dump it to a JS object.





                // controller.php (JSON encode)
                $f3->set('data',json_encode($data));




                <!-- template.html -->
                <script>
                var data={{ @data | raw }};
                </script>


                Pros: easy to use.



                Cons: your IDE will report a syntax error + extra call to raw.



                Technique 2:



                Convert data to JSON, dump it to a JS string and parse it.





                // controller.php (JSON encode + escape double quotes)
                $f3->set('data',str_replace('\u0022','\\u0022',
                json_encode($data,JSON_HEX_APOS|JSON_HEX_QUOT)));




                <!-- template.html -->
                <script>
                var data=JSON.parse('{{ @data | raw }}');
                </script>


                Cons: less easy to use + extra call to raw.



                Pros: your IDE will not report any syntax error.



                Technique 2bis:



                Embed technique 2 in a F3 template filter.





                // index.php
                $tpl=Template::instance();
                $tpl->filter('safejson',function($data){
                $raw=View::instance()->raw($data);
                return str_replace('\u0022','\\u0022',
                json_encode($raw,JSON_HEX_APOS|JSON_HEX_QUOT));
                });




                <!-- template.html -->
                <script>
                var data=JSON.parse('{{ @data | safejson }}');
                </script>


                Pros: easy to use + your IDE will not report any syntax error.



                Cons: extra call to raw.



                Technique 3:



                Convert data to JSON and embed it inside a DOM data- attribute.





                // controller.php (JSON encode)
                $f3->set('data',json_encode($data));




                <!-- template.html -->
                <div id="foo" data-json="{{ @data }}"></div>
                <script>
                var data=JSON.parse(document.getElementById('foo').dataset.json);
                </script>


                Pros: easy to use + your IDE will not report any syntax error + no extra call to raw.






                share|improve this answer














                It depends on the content stored inside $value.



                If it's a basic string with no single/double quote inside, then the following code will work:



                <script>
                var value='{{ @value }}';
                </script>


                If it's an integer, the following code will work:



                <script>
                var value={{ @value }};
                </script>


                ... although your IDE will probably report a syntax error.



                If it's a float, the following code will work:



                <script>
                var value={{ str_replace(',', '.', @value) }};
                </script>


                ... and your IDE will also probably report a syntax error. NB: the str_replace is for non-English locales that have decimal separator set to comma.



                For all the rest (strings including quotes or arrays), you should convert your data to JSON, using one of the following techniques:



                Technique 1:



                Convert data to JSON and dump it to a JS object.





                // controller.php (JSON encode)
                $f3->set('data',json_encode($data));




                <!-- template.html -->
                <script>
                var data={{ @data | raw }};
                </script>


                Pros: easy to use.



                Cons: your IDE will report a syntax error + extra call to raw.



                Technique 2:



                Convert data to JSON, dump it to a JS string and parse it.





                // controller.php (JSON encode + escape double quotes)
                $f3->set('data',str_replace('\u0022','\\u0022',
                json_encode($data,JSON_HEX_APOS|JSON_HEX_QUOT)));




                <!-- template.html -->
                <script>
                var data=JSON.parse('{{ @data | raw }}');
                </script>


                Cons: less easy to use + extra call to raw.



                Pros: your IDE will not report any syntax error.



                Technique 2bis:



                Embed technique 2 in a F3 template filter.





                // index.php
                $tpl=Template::instance();
                $tpl->filter('safejson',function($data){
                $raw=View::instance()->raw($data);
                return str_replace('\u0022','\\u0022',
                json_encode($raw,JSON_HEX_APOS|JSON_HEX_QUOT));
                });




                <!-- template.html -->
                <script>
                var data=JSON.parse('{{ @data | safejson }}');
                </script>


                Pros: easy to use + your IDE will not report any syntax error.



                Cons: extra call to raw.



                Technique 3:



                Convert data to JSON and embed it inside a DOM data- attribute.





                // controller.php (JSON encode)
                $f3->set('data',json_encode($data));




                <!-- template.html -->
                <div id="foo" data-json="{{ @data }}"></div>
                <script>
                var data=JSON.parse(document.getElementById('foo').dataset.json);
                </script>


                Pros: easy to use + your IDE will not report any syntax error + no extra call to raw.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 at 16:37

























                answered Nov 12 at 12:26









                xfra35

                3,2231216




                3,2231216






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249563%2fpass-variables-from-controller-to-javascript-function-in-fat-free-framework%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Full-time equivalent

                    さくらももこ

                    13 indicted, 8 arrested in Calif. drug cartel investigation