Useful techniques in Julia web development
In this section, we will highlight some methods and techniques that you will see used often in Julia web development and that we will also use in the project in Part 2. Here is a list of these key techniques:
- Multi-line strings: These strings are delineated with
"""
. These are often useful in web apps to use chunks of HTML code in a variable as follows:form = """ <form action="/" method="POST" enctype="multipart/form-data"> <input type="text" name="name" value="" placeholder="What's your name?" /> <input type="submit" value="Greet" /> </form> """
- String substitution: This can be used to insert variable contents into messages on the screen as follows:
<h4 class="container">
Sorry, no results were found for "$(params(:search_movies))"
</h4>
- do block syntax: This syntax makes the code easier to read. Here is a simple example:
list = [1, 2, 3]
map(x -> x^2, list)
This can also be written as follows:
map(list) -> do x x^2 end
- Here is an example usage from the Genie framework:
route("/hello.html") do
html("Hello World")
end
Here, route()
and html()
are functions from the Genie framework. The do x
syntax creates an anonymous function with argument x
and passes it as the first argument to the function stated before do
. In this example, the string "Hello World"
will be shown on the web page when the /hello.html
URL is requested.
Another way to write this, which is clearly not that readable, is as follows:
route(html("Hello World"), "/hello.html")
We will be using this do
syntax quite often in Part 2.
&&
: The Boolean and&&
operator is often used to write concise conditional code, for example:isempty(strip(params(:search_movies)))
&& redirect(:get_movies)
The preceding code first evaluates the isempty(strip(params(:search_movies)))
part; if this is false, nothing happens anymore. Only if the first part is true will the second, redirect(:get_movies)
, get evaluated. Thus, if the search_movies
parameter has a value, only then will the redirect to :get_movies
take place.
||
: The or||
operator doesn’t evaluate the part after||
when the first part is true, for example:isa(getfield(m, field), Int) || return ValidationResult(invalid, :is_int, "should be an int")
In the preceding example, if m
is of type Int
, the ValidationResult
from the right-hand side is not shown.
- Pipe operator: This operator, denoted by
|>
, is quite handy. An example is as follows:h1("Welcome Admin") |> html
The output of the preceding function before the pipe operator (|>
) is given as a first argument to the function after the pipe. This allows for easy function chaining.
- Ternary form: An
if condition a else b end
statement is often written in a ternary form:condition ? a :
b
.
An example is as follows:
flash_has_message() ? """<div class="alert alert-$flashtype alert-dismissable">$(flash())</div>""" : ""
If the function flash_has_message()
returns true
, then the multiline string after ?
(which contains the HTML code for a div
) is the result of the expression; if false the empty string ""
is returned.
- Symbols: These are often used in Julia (web) code denoted by
:
, for example:julia> sym = :info
:info
julia> typeof(sym)
Symbol
Symbols are used to indicate access to a variable (such as info
), but what info
exactly contains is not evaluated at that moment in code. A symbol gets replaced with the value bound to that symbol when the expression containing the symbol is evaluated at runtime.
Here is an example from Genie passing the values of the:user_id
and :user_status
variables in the payload to a createUser
function:
createUser(postpayload(:user_id), postpayload(:user_status, "active"))
So, postpayload(:user_id)
can be seen as binding values to variables (something that can change) inside user-provided expressions. Here, on evaluation, :user_id
is replaced by the value Symbol
is pointing to.
<% %>
: Web frameworks often embed code inside HTML with<% %>
. This can be used in the Julia Genie web framework, as follows:<h4><% movie.description %></h4>
In the preceding code, <% %>
is used to insert a description
field of a movie
instance in that place.
<% %>
can also contain whole blocks of code as well as function calls.
Embedded code can also contain a call to the @yield
macro, like in the following snippet from app.jl.html
in Genie:
<body> <div class="container"> <% @yield %> </div> </body>
@yield
is used to output the content of the view/controller into the layout.
Do familiarize yourself with the techniques described in this section. Doing so will make Julia web code instantly more understandable. All Julia web apps are projects that contain modules and use packages, so that’s what we’ll discuss in the next section.