Usage of svg() helper

Hi all,

I am trying to understand the svg() helper. Is it supposed to render a valid <svg> tag?

Nothing happens when I use it this way in my template:
<?php svg($site->file('mysvg.svg')) ?>
<?= svg($site->file('mysvg.svg')) ?>

Any help appreciated,
Matt

The svg() helper takes an absolute or relative path as parameter. You are passing a File object.

E.g.

echo svg('assets/images/logo.svg')
echo svg($site->file('mysvg.svg')->root());

thanks @texnixe and @bnomei

I tried
<?php echo svg($site->file('myfile.svg')->root()) ?>
<?php echo svg($site->file('myfile.svg')->url()) ?>
but nothing gets rendered.

Even when I use the explicit path/url of the SVG, as @texnixe suggested, nothing gets rendered in my HTML. The console shows no error either.

:frowning:

The svg helper only accepts a path, not the root or anything like that, so something like

echo svg('content/'.$site->image('my file.svg')->filename());

works for me, and getting a file from the assets folder like outlined above.

I can’t find a File method that returns the relative path content/filename…

But to achieve the same as what the svg() helper does, you might as well just do this:

<?php
if($image = $site->file('myfile.svg')):
  echo $image->read();
endif
2 Likes

@texnixe great!

both of your proposals work now as expected :ok_hand:t3:
I will stick with the $image->read() method, as it feels more straightforward to me.

but I would suggest to update the svg() docs (when there is some time left :wink: ) because I assume other people will be confused, too.
or a short cookbook episode showing the use of image files in templates in general?
for me SVGs are crucial elements of websites as I use thsi format for all icons and even some logos.