Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Mojolicious: rendering images located outside of public directory

by Anonymous Monk
on Jun 19, 2014 at 07:14 UTC ( #1090406=note: print w/ replies, xml ) Need Help??


in reply to Mojolicious: rendering images located outside of public directory

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">


Comment on Re: Mojolicious: rendering images located outside of public directory
Download Code
Re^2: Mojolicious: rendering images located outside of public directory
by Kyshtynbai (Acolyte) on Jun 19, 2014 at 07:26 UTC
    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' ) -%>">
        That's great! Thanks a lot!
        One more dumb question: Here in this code
        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' ) -%>">
        The section Data. How do I address it from route defined above? It doesnt have name like gallery.html.ep or something...
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1090406]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (13)
As of 2015-07-03 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (52 votes), past polls