Kyshtynbai has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I'm still playing with Mojolicious and I have the following question:

Can I somehow render within template images, which are stored outside of /public/images directory (let's say they are located in /private/images) ? I can render_static them on a request, but this method seems to give just the file itself, without surrounding html markup.

Thanks in advance!
  • Comment on Mojolicious: rendering images located outside of public directory

Replies are listed 'Best First'.
Re: Mojolicious: rendering images located outside of public directory
by Anonymous Monk on Jun 19, 2014 at 07:14 UTC

    Can I somehow render within template images, which are stored outside of /public/images directory (let's say they are located in /private/images) ? I can render_static them on a request, but this method seems to give just the file itself, without surrounding html markup.

    question doesn't make sense :) if you can render_static them on a request, then that is all there is to do <img src="....path that does render_static for this image">

      I'm probably doesn't made it clear, but I'm still a bit confused with frameworks concept. Let me explain: Here's code which returns just a plain image:
      $self->render_static($path_to_private_images);
      How do I insert this into <img src>? I still don't get it :).

        How do I insert this into <img src>? I still don't get it :).

        src takes a path string, which part is render_static under? path path path path path path path path path path path path :)

        see Re^2: Mojolicious, layouts and content generating. its tested, below isn't tested (template missing)

        use Mojolicious::Lite; get '/render_static/:img' => sub { my $self = shift; my $img = $self->param('img'); my $path_to_private_images = ...( $img ); return $self->render_static( $path_to_private_images ); } __DATA__ .. <img src="<%= url_for( '/render_static/yo.png' ) -%>">
        You can do one of two things. I don't know Mojo, but it's no different conceptually than what you'd do with other similar frameworks (e.g., Dancer or CGI::Application).

        First, you can put inside of the img tag the URL that Mojo will handle by delivering the static binary image content directly ( don't forget to set content type ). For example, http://path/to/mojo/getImage/<imageID>

        Second, you can have your image in a public directory available directly to any web browser. You would then just populate the img's src attribute with this URL using a template or directly generating the content. For example, http://path/too/image.jpg

        It seems to me that you're interested in doing the former - having Mojo deliver the binary content of an image (or some file) that is residing in a private directory. This approach requires that Mojo have at least 2 route handers - one to deliver the HTML and one to deliver the image's binary content.

        I suppose if you want to get fancy, you could embed the binary content directly in the HTML, but I wouldn't recommend it since it's a very inflexible approach that is typically used only for optimizing certain situations.