Pantrypoints Pantrypoints

Function Plugs and Module Plugs

April 22, 2022 2 minutes  • 313 words
Table of contents

A Plug manipulates data in conn structs. It’s a Module that has a ‘Conn’ Struct. It takes and returns that conn struct between modules.

It allows state management when combined with Agents and Genservers (like Live View). The state is held by the struct and then is passed between Modules. The ability to pass data turns Plugs into Elixir webservers

It has two types:

  • Module Plug
  • Function Plug

These are functions that get a Plug.Conn struct and give a Plug.Conn struct.

Module Plugs

These need to be initialized.

This is a large function, as a Module, that needs to be initialized with additional data such as state (i.e. initial state). This makes it more complicated than a function plug


defmodule App.Modulename do
  import Plug.Conn

  def init(options) do

  def call(conn, _options) do 



alias ModulePlugName
plug ModulePlugName, [option: "Blah"] when action is [:index]

alias Appname.Plugname



Function Plugs

These add a conn to a Module function. It is a simple function that receives a conn struct, manipulates it, and outputs the modified conn struct

How to Create Function Plugs

  1. Create new Module “test” with import Plug.conn
  2. Write the function def function_plug(conn, _options) that uses assign()
  3. In the controller, use that Function Plug Module by importing it
  4. Call function plug :blah
  5. Render it in the view with <%= @conn.assigns[:blah] %>

Function Plugs take 2 items:

  • conn
  • options

If data is available in conn then use function plugs.


defmodule App.TotalController do
  import App.ModuleName
  plug :get_total

Setting non_piped and piped Function Plugs

defmodule App.ModuleName do
  import Plug.Conn

  def non_pipe(conn, options) do 
    assign(conn, :non_pipe, 123)

  def piped(conn, _options) do
    |> put_resp_content_type("text/plain") 
    |> send_resp(200, "From a Piped Function Plug that outputted this data inside this conn struct")


Calling in Elixir

plug :non_piped

Calling in Phoenix View

<%= @conn.assigns[:non_piped] %>
<%= @non_piped %>


assign(key: "#{data.attrib}")
Follow Us! →

We're creating a new Economic System from a new Economic Science! Please support us by leaving your email or leaving a comment above.