Search code examples
phplithium

Inline SVG in Lithium View


Total newbie to Lithium.

I'm trying to inline an SVG file in a Lithium View.

In previous PHP Frameworks I would just do the following:

<?php echo file_get_contents('images/styleguide/left-arrow.svg'); ?>

In Lithium this will throw a pretty epic error with regards to where it's looking for the file:

Warning: file_get_contents(images/styleguide/left-arrow.svg): failed to open stream: No such file or directory in //app/resources/tmp/cache/templates/template_styleguide_index.html_17440333_1480885998_1509.php

Because of this location I'm wondering what the "lithium way" of doing this would be (if at all).


Solution

  • I found a solution to this (please add answers if there is a better solution).

    Use the Media class to get the root or your static files:

    use lithium\net\http\Media;
    
    $webrootPath = Media::webroot(true);
    

    Using this you can get the file contents:

    $watchRound = file_get_contents($webrootPath.'/images/styleguide/watch-round.svg');
    

    Then inline it:

    <button class="toolbar__btn js-styleguide-viewport-watch-round">
      <?php echo $watchRound; ?>
    </button>