URL Parser Module

Tram Ho

To build a real Web App , of course we’ll want to display different content with different Url , not just static links like the homepage / or /about page but what about links that contain query parameters such as /search?q=seiza .

To create such navigation logic based on links containing query parameters, we need to learn how to use some more tools provided by the package elm/url that we installed earlier. And here, we will continue to look at the example isoloaded by Elm in a series of tutorials on their official website.

Example 1

Let’s say we have a website and the path listed below are all valid.

  • /topic/architecture
  • /topic/painting
  • /topic/sculpture
  • /blog/42
  • /blog/123
  • /blog/451
  • /user/tom
  • /user/sue
  • /user/sue/comment/11
  • /user/sue/comment/51

So we have topic category pages, blog post pages, user profile pages, and methods to search for user comments. And now we have the example code that uses the module Url.Parser to write a program that parses the Url path as follows:

We have a routeParser function that takes no input parameters and will return a Parser initializer with the specific style information Parser (Route -> a) a . All the functions that support s , </> , string , int , are import from the module Url.Parser and are designed to have a syntax used at the code surface that corresponds to the format of the path that we need to split. get query parameters.

Ok, so with the syntax to represent the path type, we need to separate the parameters as above, then we have:

  • Static strings in path are described by function calls s
  • Function calls </> correspond to the slashes / in path
  • And the functions string , int , describe where to split query parameters and return data type

And this is the output of the Parser returned by the routeParser function.

Reading the definition code of the functions used in the module Url.Parser also important but not necessary at the outset, as we have seen the syntax used in a very declarative visual description, easy to understand and apply without having to read the specific sequence of operations of the function calls here.

Example 2

Now another example case where we have a personal blog with the following valid links:

  • /blog/12/the-history-of-chairs
  • /blog/13/the-endless-september
  • /blog/14/whale-facts
  • /blog/
  • /blog?q=whales
  • /blog?q=seiza

In this case, we have article pages /blog/number/ and search results pages /blog?q=... with the query parameter q= . To separate the optional query explorer parameters at the key position q= , we need to use the module Url.Parser.Query .

The syntax applied in this case is also completely visible to the type of path described in the code. And this is the output of the Parser returned by the routeParser function.

Example 3

And the last example is that we have a website in the form of a general document page with the following valid links:

  • /Basics
  • /Maybe
  • /List
  • /List#map
  • /List#filter
  • /List#foldl

In this case, we are having paths containing the id identifiers of a certain HTML element. And to separate these id , we will need to use the fragment function in the module Url.Parser .

So we have the </> fragment identity function calls to start extracting the #id at the end of the path , which is a bit special. However we can also read in a simple way, </> fragment is the # symbol, and identity is the position of the id string to be split in the path . And here is the corresponding performance result.

So we also know how to use the module Url to separate the query parameters in the path so that we can use it for processing logic in the update function of Browse.application .

The update then creates a new model record corresponding to the query information so that the view can adjust the display of the single page to suit the user’s requirements. The rest of the story now is that we don’t have a way to change the content of the <head> element yet because Elm doesn’t have any module to support it.

[Declarative Programming + Elm] Lesson 17 – JavaScript Interop

Share the news now

Source : Viblo