yaBliznyk

yaBliznyk

How to write correct view helper

defmodule Promo.OrderView do
  use Promo.Web, :view
 alias Promo.Page

 def page_title(page = %Page{}) do
    for {tag, class} <- [h5: "hide-on-small-only", h6: "hide-on-med-and-up"] do
      content_tag tag, class: class do
        page.title <> page_sub_title(page.sub_title)
      end
    end
  end

   def page_sub_title(nil), do: ""
   def page_sub_title(sub_title) do
     "<br><small>" <> sub_title <> "</small>"
  end
end    

In view i see escaped html string with small and etc.
How to write helper function “page_title” what return to me:
h2 title + br + small sub_title /small /h2, but
only h2 title /h2 if sub_title do not exists??

Marked As Solved

yaBliznyk

yaBliznyk

I find correct answer. Thanks for help

def page_title(page = %Page{}) do
    for {tag, class} <- [h5: "hide-on-small-only", h6: "hide-on-med-and-up"] do
      content_tag tag, class: class do
        do_page_title(page.title, page.sub_title)
      end
    end
  end

  defp do_page_title(title, nil), do: title
  defp do_page_title(title, sub_title) do
    ~e{<%= title %><br><small><%= sub_title %></small>}
  end

Also Liked

OvermindDL1

OvermindDL1

For any view helpers, first I have these at the top of my module:

  import Phoenix.HTML
  import Phoenix.HTML.Form
  import Phoenix.HTML.Link
  import Phoenix.HTML.Tag

Just for convenience sakes.

However, to do what you want:

  def page_sub_title(nil), do: ""
  def page_sub_title(sub_title) do
    ~e"""
    <br><small><%= sub_title %></small>
    """
  end

The ~e sigil when you import the Phoenix view stuff gives you inline ‘eex’ templating, just like eex files. This is the best way to work with it.

However, you can always wrap your output in a {:safe, content} tuple, but that is not recommended unless you are sure it is entirely safe, and considering I do not see where sub_title is coming from (possibly user input through database or so), I’d use eex to make sure everything gets properly escaped or bad bad bad things can happen.

Where Next?

Popular in Questions Top

New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement