Template folder and static files

968 views
Skip to first unread message

Ryan Chan

unread,
Sep 26, 2010, 9:50:03 AM9/26/10
to Mojolicious
Hello,

I have created an example app using the generate command,

e.g.
=============================
mojo generate app
[mkdir] /tmp/my_mojo_app/script
[write] /tmp/my_mojo_app/script/my_mojo_app
[chmod] my_mojo_app/script/my_mojo_app 744
[mkdir] /tmp/my_mojo_app/lib
[write] /tmp/my_mojo_app/lib/MyMojoApp.pm
[mkdir] /tmp/my_mojo_app/t
[write] /tmp/my_mojo_app/t/basic.t
[mkdir] /tmp/my_mojo_app/log
=============================


And in the my_mojo_app, I have a function:

get '/' => sub {
my $self = shift;

$self->render('index');
};

1. Even I have created the templates folder and put the file
index.html.ep inside it, still document not found, any idea?

2. Where to put the static files? e.g. test.css? I have created a
public folder and put the test.css inside it, still document not
found.


Thanks for such a good framework.


Dotan Dimet

unread,
Sep 26, 2010, 11:29:46 AM9/26/10
to mojol...@googlegroups.com
Ryan,

It looks like you generated a Mojo app, but added a Mojolicious::Lite
function to the my_mojo_app.pm file.
This won't work. a Mojo app also doesn't understand templates.

What you want is to generate an application skeleton with the
mojolicious command, either

mojolicious generate app
or
mojolicious generate lite-app (probably this)

There are 3 flavors of apps that can be generated by the commands in
this module:

1 - A Mojo app - you have a single "handler" function that gets a
Mojo::Transaction object; this is pretty low-level, and you work
directly with the HTTP objects - no template logic is built in, but it
does give you a script that can run as a daemon, FastCGI or CGI script.
Don't use this.

2 - a Mojolicious app - Mojolicious is a higher-level (M)VC framework
built on top of Mojo; it has conventions for template rendering and
serving static files (from a directory called "static" by default, or it
can be configured), and other niceties. It

3 - a Mojolicious::Lite app - Mojolicious::Lite provides a layer of
syntactic sugar on top of Mojolicious that is ideal for writing
single-file scripts, but still lets you access all the power of the
Mojolicious framework. It allows you to use syntax like:

'/' => sub {
my $self = shift;
$self->render('index');
}

The mojo command generates the first kind, the mojolicious command
generates the 2 other kinds.

- Dotan


--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Sebastian Riedel

unread,
Sep 26, 2010, 12:39:15 PM9/26/10
to mojol...@googlegroups.com
I can see how the current organization of the command system might be a bit confusing, so we are going to standardize on the "mojo" command for everything in the future and move all generator commands into the Mojolicious namespace. :)

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

Ryan Chan

unread,
Sep 26, 2010, 12:59:39 PM9/26/10
to Mojolicious
Hi,

On 9月26日, 下午11時29分, Dotan Dimet <do...@corky.net> wrote:
> mojolicious generate app
> or
> mojolicious generate lite-app (probably this)
>
> There are 3 flavors of apps that can be generated by the commands in
> this module:


Now I use mojolicious generate lite-app to generate the a lite app,
but also don't have templates and public folder.

So do you mean liteapp does not understand templates?

I found lite app is really handy to use, except I want to have public
and templates stuffs.

Thanks.

Ryan Chan

unread,
Sep 26, 2010, 1:09:31 PM9/26/10
to Mojolicious


On 9月27日, 上午12時59分, Ryan Chan <ryanchan...@gmail.com> wrote:
> I found lite app is really handy to use, except I want to have public
> and templates stuffs.
>

I found if I put my lite app script under the script folder, the
templates and public does not work.

If I put my lite app in the root folder, then templates and public
works, so it seems solved my problem (but I am not sure if this is the
correct way to do it).

Thanks

Ulrich Habel

unread,
Oct 6, 2010, 11:30:54 AM10/6/10
to mojol...@googlegroups.com
Hej Ryan,

> If I put my lite app in the root folder, then templates and public
> works, so it seems solved my problem (but I am not sure if this is the
> correct way to do it).

actually - yep. That's one way to do it :)

If you are playing around with Mojolicious you should start with
Mojolicious::Lite.

Let's walk through a small example:

rhaen@deploy:~/example$ mojolicious generate lite_app hello_world.pl
[exist] /home/rhaen/example
[write] /home/rhaen/example/hello_world.pl
[chmod] hello_world.pl 744


Now you have a basic application which is ready to serve webpages. You
can fire up a webserver by running the command:

./hello_world.pl daemon


Wohoo - you can access your webserver at http://localhost:3000


Now let's have a small look inside the file "hello_world.pl"


-----------8<--------------code snippet------------8<--------


#!/usr/bin/env perl

use Mojolicious::Lite;

get '/' => 'index';
[...]

@@ layouts/funky.html.ep
<!doctype html><html>
<head><title>Funky!</title></head>
<body><%== content %></body>
</html>

-----------8<--------------code snippet------------8<--------

there are some templates included already. Change them, start the
server again and see how the content of the templates can be changed
easily! You can inflate the templates into a structure - the
./hello_world.pl command will handle this for you.


rhaen@deploy:~/works$ ./hello_world.pl inflate
[mkdir] /home/rhaen/works/templates/layouts
[write] /home/rhaen/works/templates/layouts/funky.html.ep
[exist] /home/rhaen/works/templates
[write] /home/rhaen/works/templates/index.html.ep


Voila - now you have the template structure you were looking for! The
inflated files will be served before the files inside the __DATA__
section. So if you want to change your index.html.ep - please do that
inside the filesystem.

Now you have enough to get started. Visit the wiki pages of the
project (http://github.com/kraih/mojo/wiki) and try the existing
guides.


Have fun!

--
Ulrich Habel
blog: http://blog.pkgbox.org
home: http://www.pkgbox.org

Reply all
Reply to author
Forward
0 new messages