Often we want to include SVGs in our project because they load fast and can be easily manipulated using CSS. In this example, we use the Twig include
function to add svg code to our template inline. This method can be used to include any snippet of HTML that needs to be accessed across templates.
- Create a folder called images in your theme root.
$ cd MYDRUPAL
$ mkdir themes/custom/acme/images
- Copy and paste svg code into a file called mysvg.svg inside your newly created images folder
$ touch themes/custom/acme/images/mysvg.svg
You can use an svg on your machine or the example below.
<svg>
<rect x="10" y="10" height="100" width="100" style="stroke:#ff0000; fill: #0000ff"/>
</svg>
-
Paste the following code into .twig file.
{% include active_theme_path() ~ '/images/mysvg.svg' %}
-
View a page that include the template you modified.
- Can other types of files can be included?
- Where does the function
active_theme_path()
come from?