十六进制程序包中Phoenix助手的位置 [英] Location for a Phoenix helper in a hex package

查看:71
本文介绍了十六进制程序包中Phoenix助手的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了另一个 phx.gen.html ,它使用TailwindCSS创建模板.它工作正常.我想通过创建一个十六进制包 phx_tailwind_generators 来共享它.这是我的票价:

I created an alternative phx.gen.html which creates templates with TailwindCSS. It works fine. I'd like to share it by creating a Hex package phx_tailwind_generators. Here is what I have so fare:

$ phx_tailwind_generators:main> tree
.
├── README.md
├── lib
│   ├── phx_tailwind_generators.ex
├── mix.exs
├── priv
│   └── templates
│       └── tailwind.gen.html
│           ├── controller.ex
│           ├── controller_test.exs
│           ├── edit.html.eex
│           ├── form.html.eex
│           ├── index.html.eex
│           ├── new.html.eex
│           ├── show.html.eex
│           └── view.ex
└── test
    ├── phx_tailwind_generators_test.exs
    └── test_helper.exs

在这些模板中,我使用 tailwind_error_tag/2 帮助器,该帮助器在此处定义:

In those templates I use the tailwind_error_tag/2 helper which is defined here:

defmodule ExampleWeb.TailwindHelper do
  use Phoenix.HTML
  import ExampleWeb.ErrorHelpers

  @doc """
  Generates tag for inlined form input errors.
  """
  def tailwind_error_tag(form, field) do
    Enum.map(Keyword.get_values(form.errors, field), fn error ->
      content_tag(:p, translate_error(error),
        class: "mt-2 text-sm text-red-500",
        phx_feedback_for: input_name(form, field)
      )
    end)
  end
end

但是如何将该帮助程序定义存储在十六进制包中?如何重命名 defmodule ExampleWeb.TailwindHelper ,使其能够在目标系统中正常工作?

But how do I store this helper definition in the hex package? How do I rename defmodule ExampleWeb.TailwindHelper do in a way that it will work in the target system?

回购: https://github.com/wintermeyer/tailwind_phx_generators

推荐答案

为了让您包的用户使用此帮助程序,他们将需要在其 def视图部分中导入模块 myapp_web.ex 文件.将模块命名为适合您的项目,并在自述文件中为其提供说明.您可以检出的示例项目为: https://github.com/ikeikeikeike/phoenix_html_simplified_helpers .

In order for users of your package to use this helper, they will need to import the module in the def view section of their myapp_web.ex file. Name the module as fitting for your project and in your Readme give them the instructions. An example project you can check out would be: https://github.com/ikeikeikeike/phoenix_html_simplified_helpers.

自述文件中的语录:

3 phoenix_html_simplified_helpers需要导入(使用)Phoenix项目.以下说明是在web.ex中添加使用语法".

3 phoenix_html_simplified_helpers need to import(use) your Phoenix project. The following description is adding 'use syntax' into web.ex.

def view do
  quote do
    use Phoenix.View, root: "web/templates"

    # Import convenience functions from controllers
    import Phoenix.Controller, only: [get_csrf_token: 0, get_flash: 2, view_module: 1]

    # Use all HTML functionality (forms, tags, etc)
    use Phoenix.HTML
    use Phoenix.HTML.SimplifiedHelpers  # <- this line.

    import MyApp.Router.Helpers
    import MyApp.ErrorHelpers
    import MyApp.Gettext
  end
end

这篇关于十六进制程序包中Phoenix助手的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆