1. Perl
  2. Mojolicious

Creating a Web application template

Mojolicious will automatically generate the minimum required template. I will explain how to create a template and the template of the created Web application.

Creating a Web application template

To create a web application template, change to the directory directly under mojo and execute the following command. It seems to be a good practice to keep the application name in uppercase and the letters after it in lowercase. (Names like "TestApp" aren't very good, I think names like "Testapp" are good.)

# For Windows
perl script\mojo generate app web application name

# For Unix-like OS
perl script/mojo generate app web application name

Now let's create a template with the web application name named Exampleapp.

# For Windows
perl script\mojolicious generate app Example app

# For Unix-like OS
perl script/mojolicious generate app Example app

A list of created files will be output to the screen.

  [mkdir] C:\Download\mojo\exampleapp\script
  [write] C:\Download\mojo\exampleapp\script\exampleapp
  [chmod] exampleapp/script/exampleapp 744
  [mkdir] C:\Download\mojo\exampleapp\lib
  [write] C:\Download\mojo\exampleapp\lib\Exampleapp.pm
  [mkdir] C:\Download\mojo\exampleapp\lib\Exampleapp
  [write] C:\Download\mojo\exampleapp\lib\Exampleapp\Example.pm
  [mkdir] C:\Download\mojo\exampleapp\t
  [write] C:\Download\mojo\exampleapp\t\basic.t
  [mkdir] C:\Download\mojo\exampleapp\log
  [mkdir] C:\Download\mojo\exampleapp\public
  [write] C:\Download\mojo\exampleapp\public\index.html
  [mkdir] C:\Download\mojo\exampleapp\templates
  [write] C:\Download\mojo\exampleapp\templates\not_found.html.ep
  [exist] C:\Download\mojo\exampleapp\templates
  [write] C:\Download\mojo\exampleapp\templates\exception.html.ep
  [mkdir] C:\Download\mojo\exampleapp\templates\layouts
  [write] C:\Download\mojo\exampleapp\templates\layouts\default.html.ep
  [mkdir] C:\Download\mojo\exampleapp\templates\example
  [write] C:\Download\mojo\exampleapp\templates\example\welcome.html.ep

Role of each created directory

If you specify the name "Exampleapp", a directory called "exampleapp" will be created under mojo. You will create a web application by editing the files under exampleapp. I will explain the role of each directory to get an overall overview.

exampleapp-+
           | -stores the lib application
           | -log Store error and warning logs
           | -public Store static files (HTML, CSS, JavaScript)
           | -script Stores HTTP server startup scripts, etc.
           | -t Store web application exams
           | -templates Store HTML templates

I will briefly explain the file.

lib - directory to store applications

The lib directory stores applications. An application is a module written in Perl in Mojolicious. Therefore, the .pm file will be stored under the lib directory.

The default files under lib are as follows.

lib-+
     | -Exampleapp.pm
     | -Exampleapp /-+
                    |-Example.pm

Exampleapp.pm is a class that represents the application itself. In this, the settings related to the application and the branching of processing for each URL are described.

Classes that will be controllers are placed under the Exampleapp directory. The controller class is a class that describes the process corresponding to the URL. Remember that the controller describes the process that corresponds to the URL. (It corresponds to C of MVC which is often called)

log - Store error and warning logs

Mojolicious messages are output to a file called development.log under the log directory. If you get any error, look at this log.

public - Store static files (HTML, CSS, JavaScript)

This directory contains static files such as HTML files, CSS, and JavaScript.

script - Stores HTTP server startup scripts, etc.

Below the script, the script that starts the HTTP server is placed. Mojolicious implements an HTTP server for convenience such as testing. By starting this HTTP server, you can run the web application without an HTTP server such as Apache.

t - Store web application exams

The test of the Web application is stored under t. Testing web applications is generally a hassle, but Mojolicous has a testing framework called Test::Mojo that makes it easy to test.

templates - Store HTML templates

Mojolicious has Mojo::Template as a standard template engine. This corresponds to V in MVC. You can use Mojo::Template to dynamically generate HTML on request. Unlike HTML::Template and Template-Toolkit, Mojo::Template is characterized by being able to use Perl syntax in templates.

The Mojolicious template has a ".ep" extension. Let's take a closer look at the directory structure of templates. It is as follows.

templates + example-+
          | |-welcome.html.ep - Templates for HTML bodies
          |
          + layouts-+
          | |-default.html.ep - Templates for HTML headers
          |
          + exception.html.ep
          |
          + not_found.html.ep

Notice that there is a directory called layouts. This is commonly used as a template for HTML headers. exexample/welcome.html.ep is a template corresponding to the action "welcome" of the controller "Exexample". I will explain the action later.

exceptions.html.ep is a template for exceptions. Mojolicious uses this template to display error messages on the screen in an easy-to-understand way, which makes development very easy. not_found.html.ep is a template used when a page is not found.

Related Informatrion