elixir mjml_nif 试用

发布时间 2023-12-28 20:39:21作者: 荣锋亮

mjml_nif 是使用rustler 包装的mrml 基于了erlang 的nif 接口实现的elixir 模块,同时为了方便预编译nif的使用,使用了
rustler_precompiled,以下是一个简单的试用

项目试用

  • 添加依赖
 
defp deps do
    [
      {:mjml, "~> 3.0"}
    ]
end
  • 代码集成
    lib/mail/rong.mail.ex
 
defmodule Rong.Mail do
  def mail(_to,_subject,body) do
    {:ok, html} = Mjml.to_html(body)
    html
  end
end

lib/rong.ex

def mail(to,subject,body) do
    Mail.mail(to,subject,"<mjml><mj-body><mj-section><mj-column><mj-text>#{body}</mj-text></mj-column></mj-section></mj-body></mjml>")
end
  • 运行效果
iex -S mix 
Rong.mail(:dalongm,:demo,"this is a demo"

效果

说明

基于mrml 的周边包装目前也有几个,因为使用了rust,实际上我们可以将此模块扩展到其他编程语言中

参考资料

https://github.com/adoptoposs/mjml_nif
https://github.com/philss/rustler_precompiled
https://github.com/rusterlium/rustler
https://github.com/jdrouet/mrml