Include jQuery into Symfony2

前端 未结 4 658
情歌与酒
情歌与酒 2020-12-03 06:20

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

4条回答
  •  有刺的猬
    2020-12-03 06:56

    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
    

提交回复
热议问题