Passing helper functions to templates
We saw earlier in the chapter how Phoenix allows us to use functions defined in the view module inside the templates. This further allows us to clean up the templates and extract shared/complex logic in the templates, by moving it to the views. In this section, we will learn how we can use the EEx
module to pass functions to the HTML templates.
In the previous chapter, we used the first two arguments of EEx.eval_file/3
to pass a file path and list of variable bindings to the template. The third argument of this function is a list of options, one of which is :functions
. By using the :functions
key, we can pass a list of functions that are accessible to the template at the time of its evaluation. We can use this feature to pass a list of functions defined in the view module to our HTML template.
Now that we know how to pass a list of functions to a template, we just need a way to get a list of all the public functions defined in the view module...