Giter Site home page Giter Site logo

Comments (3)

stevedomin avatar stevedomin commented on July 29, 2024

Hello @sitch.

Thanks for opening an issue!

Would you mind sharing more details about your use-case? I'm struggling to see where this would be more powerful than a simple string interpolation?

Thanks!

from phoenix_swoosh.

sitch avatar sitch commented on July 29, 2024

Well firstly, rendering the subject as a template can be useful non-devs to maintain the templates. My use case is more that i am passing an assigns map to render_body/3 or to Swoosh.Email.new/1, and I can just repeat myself or i can access those assigns variables for consistency

from phoenix_swoosh.

sitch avatar sitch commented on July 29, 2024

This is what i'm using internally to do:

new()
|> assign(:name, "Sitch")
|> render_subject(~S("Welcome #{name}"))
|> render_body("template.html")
defmodule RenderingSubject do

  def render_subject(%Swoosh.Email{assigns: assigns} = struct, template) do
    subject = eval_quoted_string(template, assigns)
    %{struct | subject: subject}
  end

  def eval_quoted_string(template, assigns) when is_binary(template) and is_map(assigns) do
    with assignment_list <- Enum.into(assigns, []),
         {result, _assign} <- Code.eval_string(template, assignment_list)
    do
      result
    else
      _ -> raise "Unknown argument in .eval_quoted_string/2"
    end
  end
end

from phoenix_swoosh.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.