@@ -780,6 +780,77 @@ should render with the subdirectory (e.g. ``/my_app/images/logo.png``). The
780
780
``asset `` function takes care of this by determining how your application is
781
781
being used and generating the correct paths accordingly.
782
782
783
+ .. index ::
784
+ single: Templating; Including stylesheets and Javascripts
785
+ single: Stylesheets; Including stylesheets
786
+ single: Javascripts; Including Javascripts
787
+
788
+ Including Stylesheets and Javascripts in Twig
789
+ ---------------------------------------------
790
+
791
+ No site would be complete without including Javascript files and stylesheets.
792
+ In Symfony, the inclusion of these assets is handled elegantly by taking
793
+ advantage of Symfony's template inheritance.
794
+
795
+ .. tip ::
796
+
797
+ This section will teach you the philosophy behind including stylesheet
798
+ and Javascript assets in Symfony. Symfony also packages another library,
799
+ called assetic, which follows this philosophy but allows you to do much
800
+ more interesting things with those assets.
801
+
802
+ Start by adding two blocks to your base template that will hold your assets:
803
+ one called ``stylesheets `` inside the ``head `` tag and another called ``javascripts ``
804
+ just above the closing ``body `` tag. These blocks will contain all of the
805
+ stylesheets and Javascripts that you'll need throughout your site::
806
+
807
+ .. code-block :: html+twig
808
+
809
+ {# 'app/Resources/views/base.html.twig' #}
810
+ <html>
811
+ <head>
812
+ {# ... #}
813
+
814
+ {% block stylesheets %}
815
+ <link href="{{ asset('/css/main.css') }}" type="text/css" rel="stylesheet" />
816
+ {% endblock %}
817
+ </head>
818
+ <body>
819
+ {# ... #}
820
+
821
+ {% block javascripts %}
822
+ <script src="{{ asset('/js/main.js') }}" type="text/javascript"></script>
823
+ {% endblock %}
824
+ </body>
825
+ </html>
826
+
827
+ That's easy enough! But what if you need to include an extra stylesheet or
828
+ Javascript from a child template? For example, suppose you have a contact
829
+ page and you need to include a ``contact.css `` stylesheet *just * on that
830
+ page. From inside that contact page's template, do the following:
831
+
832
+ .. code-block :: html+twig
833
+
834
+ {# src/Acme/DemoBundle/Resources/views/Contact/contact.html.twig #}
835
+ {# extends '::base.html.twig' #}
836
+
837
+ {% block stylesheets %}
838
+ {{ parent() }}
839
+
840
+ <link href="{{ asset('/css/contact.css') }}" type="text/css" rel="stylesheet" />
841
+ {% endblock %}
842
+
843
+ {# ... #}
844
+
845
+ In the child template, you simply override the ``stylesheets `` block and
846
+ put your new stylesheet tag inside of that block. Of course, since you want
847
+ to add to the parent block's content (and not actually *replace * it), you
848
+ should use the ``parent() `` Twig function to include everything from the ``stylesheets ``
849
+ block of the base template.
850
+
851
+ The end result is a page that includes both the ``main.css `` and ``contact.css ``
852
+ stylesheets.
853
+
783
854
.. index ::
784
855
single: Templating; The templating service
785
856
0 commit comments