milof
milof

Reputation: 696

Including Zepto or jQuery at runtime in Symfony2

I am attempting to incorporate Foundation framework in Symfony2.

I copied all the css/js assets in the app/Resources folder and am able to reference them. However there is one part which is not getting including properly:

 <script>
  document.write('<script src=' +
  ('__proto__' in {} ? '../app/Resources/public/js/vendor/zepto' : '../app/Resources/public/js/vendor/jquery') +
  '.js><\/script>')
  </script>

That is I am unable to get jQuery or Zepto loaded. How can I convert the above statement so that it uses Symfony 2 assetic syntax.

Something like:

  <script>
if ('__proto__' in {})
{
  {% javascripts '../app/Resources/public/js/vendor/zepto.js' %}
    <script type="text/javascript" src="{{ asset_url }}"></script>      
{% endjavascripts %}
}

Else
{
  {% javascripts '../app/Resources/public/js/vendor/jquery.js' %}
    <script type="text/javascript" src="{{ asset_url }}"></script>      
{% endjavascripts %}
}

I know I can easily copy the foundation assets to the web folder and be done with it but I was hoping I maintain standard and am able to keep libraries hidden.

Many thanks

Upvotes: 2

Views: 204

Answers (1)

Ryan
Ryan

Reputation: 5026

This might do the trick.

<script>
var zeptoSrc = "{% javascripts '../app/Resources/public/js/vendor/zepto.js' %}{{ asset_url|e('js') }}{% endjavascripts %}";
var jquerySrc = "{% javascripts '../app/Resources/public/js/vendor/jquery.js' %}{{ asset_url|e('js') }}{% endjavascripts %}";

document.write('<script src="' + ('__proto__' in {} ? zeptoSrc : jquerySrc) + '"><\/script>');
</script>

Upvotes: 2

Related Questions