Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use Assetic in Symfony

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 339
    Comment on it

    Hello Friends,

    Assetic is used to define CSS, JS and Images location in symfony. Assetic provides you flexiblity to store CSS, JS and Images at your desirable location and you can call these files on your twig or controller file easily.  Assetic is not available by default from Symfony 2.8 Standard Edition or above. To use this features, we need to install the AsseticBundle in your project. Please use the below steps to install Assetic:

    1) To Install Assetic please open terminal and

    //enter in your project path a below

    cd /var/www/html/myproject

    composer require symfony/assetic-bundle

    2) Now we need to enable this bundle in AppKernel.php

    // app/AppKernel.php
    class AppKernel extends Kernel
    {
        public function registerBundles()
        {
            $bundles = array(
                //Add the below line in your $bundle
                new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            );
        }
    }

    Save this file.

    3) Now open "app/config/config.yml" file and add the following code

    # app/config/config.yml
    assetic:
        debug:          '%kernel.debug%'
        use_controller: '%kernel.debug%'
        filters:
            cssrewrite: ~
    

    4) Now we can enjoy assetic bundle in your template(view file)

    ############ INCLUDE JS FILE ###########

    {% block javascripts %}
        {% javascripts '@AppBundle/Resources/public/js/*' %}
            <script src="{{ asset_url }}"></script>
        {% endjavascripts %}
    {% endblock %}

    ############ INCLUDE CSS FILE ###########

    {% block stylesheets %}
        {% stylesheets 'bundles/app/css/*' filter='cssrewrite' %}
            <link rel="stylesheet" href="{{ asset_url }}" />
        {% endstylesheets %}
    {% endblock %}

    ############# INCLUDE IMAGE ############

    {% image '@AppBundle/Resources/public/images/example.jpg' %}
        <img src="{{ asset_url }}" alt="Example" />
    {% endimage %}
    

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: