-
Notifications
You must be signed in to change notification settings - Fork 14
Tutorial Parameter Passing
In this tutorial we will go over how to pass a parameter to a view.
Having a static hello world page is nice, but it would be better if we could pass a message to our hello world page to display it. To do this, we first need to change our view to accept a message and display it.
Changing our view to accept a message is easy. Open the index.clj file and change the line which looks like:
(def-view []
to
(def-view [message]
Since views are basically functions, we can simply add a parameter to the function then use it. To add a parameter to a view function, simply add it to the vector when defining the view.
Now that we have a message parameter, we can display it. Change the line:
[:p "Hello World!"]]))
to
[:p message]]))
We assume message is a string, and simply display it as the content of our view.
The last thing we need to do is pass a message to our view to display it. This requires us to update our binding.
In the src/bindings/home/index.clj file, change the the def-binding:
(def-binding [] (with-home-request-map (render-view)))
to
(def-binding [] (with-home-request-map (render-view "Hello World! (from binding)")))
If we save both files and reload our page, we should see a message from the binding.