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

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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