How to embed images and CSS as separate files

747 views
Skip to first unread message

J. Louis

unread,
Apr 12, 2010, 1:51:14 PM4/12/10
to Mojolicious
I have figured out how to embed layouts, css, but I would like to ask
is there a way to embed images and if there is better way to embed the
CSS:

First question:
The way I embed CSS is below, is there a way to embed CSS without
using routes like get '/global.css'?

Second:
Is there a way to embed images, like base64 encoded, something like
below, and below is just an idea.

Thank you.

get '/global.css' => sub { my $self = shift; $self-
>render('global'); } => 'global';

@@ layouts/template.html.ep
<link href="index.cgi/global.css" rel="stylesheet" type="text/css"/>

@@ global.css.ep
@media all{.printlogo{position.........

In this is what I am tin
@@ /files/template/tel-logo.gif

R0lGODlhXQBrAOZ/
AMa6zPBWFWomg7qrxUwVdp5UltTL19fQ2xgIRPPy8/7/9/3/8aaUuVErd3Fe
k4uIkzQpUvjVqRQHOZaOpeLc4/n5+dDG1AkEF/7+/
uzp7KGhokpHUvOLJcfIyDgWafGvW9vV3X15
hQ0FIx8LU+ro8hIJK/n2+1FDc3MvhicSVv75/uXg5fr58DUNaC0nQOnl6vHu8/z8++rMb/
fuwUc4
XCALRl5Ua7+xyVEeD2NgaP78/yIVSDUUXf///CoMYGxldrWmv

Sebastian Riedel

unread,
Apr 12, 2010, 4:54:29 PM4/12/10
to mojol...@googlegroups.com
> I have figured out how to embed layouts, css, but I would like to ask
> is there a way to embed images and if there is better way to embed the
> CSS:
>
> First question:
> The way I embed CSS is below, is there a way to embed CSS without
> using routes like get '/global.css'?

No there is not, there always needs to be a matching route, static files are supposed to go into the public directory.
Actionless templates are possible though.

get '/foo.css' => 'foo';

@@ foo.css.ep
...

> Second:
> Is there a way to embed images, like base64 encoded, something like
> below, and below is just an idea.

If you really want to do this there is render_data.

use Mojo::ByteStream 'b';
get '/foo.png' => sub { shift->render_data(b(<<EOF)->b64_decode->to_string) };
...
EOF

--
Sebastian Riedel
http://labs.kraih.com
http://mojolicious.org
http://twitter.com/kraih

J. Louis

unread,
Apr 12, 2010, 5:38:01 PM4/12/10
to Mojolicious
Dear Sebastian,

Thank you. This is very useful, but it would force me putting images
before shagadelic.

Do you suggest reading images from subroutines like:

$img = image();
#and then
get '/foo.png' => sub { shift->render_data(b($img)->b64_decode-
>to_string) };

or is there Mojolicious way to do it?

Sebastian Riedel

unread,
Apr 12, 2010, 11:05:23 PM4/12/10
to mojol...@googlegroups.com
> No there is not, there always needs to be a matching route, static files are supposed to go into the public directory.
> Actionless templates are possible though.
>
> get '/foo.css' => 'foo';
>
> @@ foo.css.ep
> ...


Think we'll add a base64 renderer to deal with embedded binary files, it would work like this.

# GET /foo.png
get '/foo' => 'foo';

@@ foo.png.b64
...

J. Louis

unread,
Apr 13, 2010, 9:45:48 AM4/13/10
to Mojolicious
That is excellent solution.

Due to the nature of Mojolicious::Lite and one-file possibility I was
thinking the functionality is there by default.

Including such a functionality is excellent, finally I can make a
single file server various domains with all what is required.

Louis

Reply all
Reply to author
Forward
0 new messages