Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Web Development with Julia and Genie

You're reading from   Web Development with Julia and Genie A hands-on guide to high-performance server-side web development with the Julia programming language

Arrow left icon
Product type Paperback
Published in Nov 2022
Publisher Packt
ISBN-13 9781801811132
Length 254 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ivo Balbaert Ivo Balbaert
Author Profile Icon Ivo Balbaert
Ivo Balbaert
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Part 1: Developing Web Apps with Julia
2. Chapter 1: Julia Programming Overview FREE CHAPTER 3. Chapter 2: Using Julia Standard Web Packages 4. Chapter 3: Applying Julia in Various Use Cases on the Web 5. Part 2: Using the Genie Rapid Web Development Framework
6. Chapter 4: Building an MVC ToDo App 7. Chapter 5: Adding a REST API 8. Chapter 6: Deploying Genie Apps in Production 9. Chapter 7: Adding Authentication to Our App 10. Chapter 8: Developing Interactive Data Dashboards with Genie 11. Index 12. Other Books You May Enjoy

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.

You have been reading a chapter from
Web Development with Julia and Genie
Published in: Nov 2022
Publisher: Packt
ISBN-13: 9781801811132
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime