{"id":23144,"date":"2024-03-07T15:55:57","date_gmt":"2024-03-07T14:55:57","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/usar-assetic-para-administrar-archivos-css-javascript-e-imagenes\/"},"modified":"2024-06-03T18:44:17","modified_gmt":"2024-06-03T17:44:17","slug":"usar-assetic-para-administrar-archivos-css-javascript-e-imagenes","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/use-assetic-to-manage-javascript-css-files-and-images\/","title":{"rendered":"Use Assetic to manage CSS, JavaScript and images files"},"content":{"rendered":"<p>In web development, one of the most common challenges is managing and organizing CSS, JavaScript, and image files efficiently. Fortunately, Symfony offers a powerful tool called Assetic that allows us to manage these files in a simple and optimized way. In this article, we will learn how to use Assetic to manage our web assets effectively.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/use-assetic-to-manage-javascript-css-files-and-images\/#%C2%BFQue_es_Assetic\" >What is Assetic?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/use-assetic-to-manage-javascript-css-files-and-images\/#Configuracion_de_Assetic_en_Symfony\" >Configuring Assetic in Symfony<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/use-assetic-to-manage-javascript-css-files-and-images\/#Uso_de_filtros_y_optimizaciones\" >Using filters and optimizations<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/use-assetic-to-manage-javascript-css-files-and-images\/#Entrega_de_activos_en_grupos_y_versionado\" >Delivering assets in groups and versioning<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/use-assetic-to-manage-javascript-css-files-and-images\/#Conclusiones\" >Conclusions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/use-assetic-to-manage-javascript-css-files-and-images\/#Preguntas_frecuentes\" >Frequently asked questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/use-assetic-to-manage-javascript-css-files-and-images\/#%C2%BFQue_es_Assetic_en_Symfony\" >What is Assetic in Symfony?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/use-assetic-to-manage-javascript-css-files-and-images\/#%C2%BFCual_es_la_ventaja_de_usar_Assetic\" >What is the advantage of using Assetic?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/blog\/use-assetic-to-manage-javascript-css-files-and-images\/#%C2%BFQue_filtros_y_optimizaciones_ofrece_Assetic\" >What filters and optimizations does Assetic offer?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/nelkodev.com\/en\/blog\/use-assetic-to-manage-javascript-css-files-and-images\/#%C2%BFComo_puedo_agrupar_y_versionar_mis_activos_en_Symfony\" >How can I group and version my assets in Symfony?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_Assetic\"><\/span>What is Assetic?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Assetic is a Symfony library that makes it easy to manage and optimize web assets such as CSS, JavaScript files, and images. It combines, compresses and processes these assets automatically, resulting in faster loading of our web pages. Assetic also allows delivering assets in groups and versioning them for browser caching.<\/p>\n<p>The main advantage of using Assetic is that it provides us with a simple way to organize and manage our web assets, avoiding the need to write large amounts of code to do it manually. Additionally, Assetic offers a large number of filters and optimizations to improve the efficiency and performance of our web applications.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Configuracion_de_Assetic_en_Symfony\"><\/span>Configuring Assetic in Symfony<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To use Assetic in Symfony, we must first configure it in our configuration file <code>config.yml<\/code>. We will add the following lines of code:<\/p>\n<pre>\nassetic: debug: &#039;%kernel.debug%&#039; filters: cssrewrite: ~ uglifyjs2: bin: &#039;%kernel.root_dir%\/..\/node_modules\/.bin\/uglifyjs&#039; uglifycss: bin: &#039;%kernel.root_dir%\/..\/node_modules\/.bin\/uglifycss&#039; assets: app_css: inputs: - &#039;%kernel.root_dir%\/Resources\/assets\/css\/style.css&#039; output: &#039;css\/app.css&#039; app_js: inputs: - &#039;%kernel.root_dir%\/Resources\/assets\/js\/script.js&#039; output: &#039;js\/app.js&#039; app_images: inputs: - &#039;%kernel.root_dir%\/Resources\/assets\/images\/**\/*&#039; output: &#039;images\/&#039;\n<\/pre>\n<p>In the code above, we have configured Assetic to process a CSS file, a JavaScript file, and an images folder. We have defined the input file paths and the output paths for the processed files.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Uso_de_filtros_y_optimizaciones\"><\/span>Using filters and optimizations<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Assetic provides a wide range of filters and optimizations that can be applied to our web assets. Some popular examples are:<\/p>\n<ul>\n<li><strong>CSSRewriteFilter:<\/strong> This filter automatically adjusts the paths of images and fonts in our CSS files so that they are correct in the context of the current path.<\/li>\n<li><strong>UglifyJsFilter and UglifyCssFilter:<\/strong> These filters compress our JavaScript and CSS code respectively, reducing their size and improving loading time.<\/li>\n<li><strong>ImageMinFilter:<\/strong> This filter optimizes images, reducing their size without significantly compromising quality.<\/li>\n<\/ul>\n<p>To use these filters, we simply add them to the Assetic configuration and assign them to the corresponding assets.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Entrega_de_activos_en_grupos_y_versionado\"><\/span>Delivering assets in groups and versioning<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Assetic allows us to group and version our assets to improve browser caching. Bundling assets means combining them into a single file to reduce the number of HTTP requests. Versioning assets means adding a version number to the file name so that when the file changes, the browser is forced to download it again instead of using the cached version.<\/p>\n<p>In Symfony, we can use the command <code>assetic:dump<\/code> to generate grouped and versioned files of our assets. This will be done automatically every time we run the command <code>app\/console cache:clear<\/code>. We can then use these files in our Twig templates using the function <code>asset<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusiones\"><\/span>Conclusions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Assetic is a great tool to manage and optimize our CSS, JavaScript and images files in Symfony. It allows us to organize and process our assets efficiently, improve the performance of our web applications, and reduce the amount of manual code we must write. Try it and enjoy better management of your web assets!<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Preguntas_frecuentes\"><\/span>Frequently asked questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_Assetic_en_Symfony\"><\/span>What is Assetic in Symfony?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Assetic is a Symfony library that makes it easy to manage and optimize web assets such as CSS, JavaScript files, and images.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFCual_es_la_ventaja_de_usar_Assetic\"><\/span>What is the advantage of using Assetic?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The main advantage of using Assetic is that it provides us with a simple way to organize and manage our web assets, avoiding the need to write large amounts of code to do it manually.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFQue_filtros_y_optimizaciones_ofrece_Assetic\"><\/span>What filters and optimizations does Assetic offer?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Assetic offers a wide range of filters and optimizations, such as CssRewriteFilter, UglifyJsFilter, UglifyCssFilter, and ImageMinFilter, among others.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFComo_puedo_agrupar_y_versionar_mis_activos_en_Symfony\"><\/span>How can I group and version my assets in Symfony?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In Symfony, you can use the assetic:dump command to generate grouped and versioned files of your assets. You can then use these files in your Twig templates using the asset function.<\/p>","protected":false},"excerpt":{"rendered":"<p>En el desarrollo web, uno de los desaf\u00edos m\u00e1s comunes es administrar y organizar los archivos CSS, JavaScript e im\u00e1genes de manera eficiente. Afortunadamente, Symfony ofrece una herramienta poderosa llamada Assetic que nos permite gestionar estos archivos de forma sencilla y optimizada. En este art\u00edculo, aprenderemos c\u00f3mo utilizar Assetic para administrar nuestros activos web de [&hellip;]<\/p>","protected":false},"author":1,"featured_media":23145,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,2207,16],"tags":[969,602,970,205,519,284,185,2208,60,101,79],"class_list":["post-23144","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-nodejs","category-programacion","tag-administrar","tag-archivos","tag-assetic","tag-blog","tag-css","tag-imagenes","tag-javascript","tag-nodejs","tag-para","tag-programacion","tag-usar"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/23144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=23144"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/23144\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/23145"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=23144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=23144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=23144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}