I am new in Symfony2 framework and not fully understand how to work with javascripts and how to include theirs in the best way.
What I need: to include jQuery script
For implementation with a composer package, navigate to your symfony package and run the following commands.
Download Vendor Package:
The package you choose is up to you, I am using a popular package (bmatzner/jquery-bundle) for this example.
php composer.phar require bmatzner/jquery-bundle:2.*
for jQuery 2.x
or
php composer.phar require bmatzner/jquery-bundle:1.*
for jQuery 1.x
Add The Bundle to your AppKernel:
/* /app/AppKernel.php */
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
new Bmatzner\JQueryBundle\BmatznerJQueryBundle(),
///...
Install Assets:
php bin/console assets:install --symlink web
To include in template:
{% block title %}Welcome!{% endblock %}
{% block stylesheets %}{% endblock %}
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
Clear Cache:
php bin/console cache:clear --env=dev
php bin/console cache:clear --env=prod