tio407

tio407

API client design: generating template code for client

Hello I am forking a project’s recaptcha code. But want to make this a little bit more readable for my local project. Here is the code. It returns a string with reCAPTCHA code, which means that the consumer will have to convert the string to HTML code using Phoenix.HTML.Raw/1, is there another way to do this? A better way perhaps, without the use of raw?

defmodule Recaptcha.Template do
  require Elixir.EEx
  alias Recaptcha.Config

  EEx.function_from_file(:defp, :render_template, "lib/template.html.eex", [
    :assigns
  ])

  def display(options \\ []) do
    public_key =
      options[:public_key] || Config.get_env(:recaptcha, :public_key)

    callback =
      if options[:size] == "invisible" && is_nil(options[:callback]) do
        "recaptchaCallback"
      else
        options[:callback]
      end

    onload =
      if options[:onload] do
        "onload=#{options[:onload]}&"
      else
        ""
      end

    options_dict = Keyword.put(options, :onload, onload)

    render_template(%{
      public_key: public_key,
      callback: callback,
      options: options_dict
    })
  end
end

The template.html.eex that it references is just standard html code with variables interpolated.

Thank you for your help.

First Post!

gregvaughn

gregvaughn

You could wrap your recaptcha in a struct and have it implement Phoenix.HTML.Safe protocol

Where Next?

Popular in Discussions Top

PragTob
Hey everyone, this has been on my mind for some time and I’d love your input on it! TLDR: I feel like maps are superioer for storing and...
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36689 110
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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

We're in Beta

About us Mission Statement