2

I'm using libraries like amcharts that have many js files.

I put amcharts in web/js/amcharts as suggested julesbou and then

{% block javascripts %}
    {{ parent() }}
    {% javascripts  '/js/amcharts/amcharts.js' %}
        <script src="{{ asset_url }}" type="text/javascript"> </script>
    {% endjavascripts %}
{% endblock %}

But I get [exception] 500 | Internal Server Error | RuntimeException [message] The source file &quot;/amcharts/amcharts.js&quot; does not exist.

0

2 Answers 2

2

Edit

To include a file under web try removing the forward slash from your path i.e.

{% javascripts  'js/amcharts/amcharts.js' %}
                 ^ remove / here
Sign up to request clarification or add additional context in comments.

6 Comments

the asset() function can be used instead of creating an asset collection with javascripts if you only reference a single css/js file. But keep in mind that the asset won't be processed by any filter this way. however it's not true that javascripts only accepts @AcmeBundle style paths.
@nifr I haven't been able to get javascripts to work with files under web. if it's possible, I actually prefer it
The {% javascripts %} tag does nothing more than configuring a new asset collection like adding a config directive assetic.assets.<asset-name>.(inputs|filters|output) does. it just returns the output-name as asset_url variable to twig with the correct current path. You can definitely add files from the web folder to an asset collection's input-array.
Further the documentation clearly states that it's not advised to put javascript/css libraries into bundles. A bundle should not embed third-party libraries written in JavaScript, CSS, or any other language.
@nifr You're right! I was able to get it to work w/o the initial forward slash
|
-1

Suppose you have put your javascripts sources named xyz.js in 'js/' folder is source then you will have to fetch this javascripts via your "app/resources/base.html" file loke this :

{% block javascripts %}
     <script src="{{ asset('js/xyz.js') }}" type="text/javascript"> </script>
{% endblock %}

and when in remaining twig file you extends base.html.twig like below :

{% extends '::base.html.twig' %} 
  {% block display %}
  {% endblockdisplay %}

it automatically fetch your javascripts . Even if you want to modify the javascripts code for that particular child twig then you will have to write like as :

{% extends '::base.html.twig' %} 

    {% block display %}
     // Your display content here
    {% endblockdisplay %}

    {% block javascripts %}
       {{ parent() }}
        // Your changing code here
    {% endblockjavascripts %}

Comments

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.