<commit_before>defmodule Phoenix.View do
  @moduledoc """
  Defines the view layer of a Phoenix application.

  This module is used to define the application main view, which
  serves as the base for all other views and templates in the
  application.

  The view layer also contains conveniences for rendering templates,
  including support for layours and encoders per format.

  ## Examples

  First, define the main view module:

      defmodule YourApp.View do
        use Phoenix.View, root: "web/templates"

        # Everything in this block is available runs in this
        # module and in other views that use YourApp.View
        using do
          # Import common functionality
          import YourApp.I18n
          import YourApp.Router.Helpers

          # Use Phoenix.HTML to import all HTML functions (forms, tags, etc)
          use Phoenix.HTML
        end

        # Functions defined here are available to all other views/templates
      end

  Once the main view module is defined, we can use it to define each view:

      defmodule YourApp.UserView do
        use YourApp.View
      end

  Because we have defined the template root to be "web/template", `Phoenix.View`
  will automatically load all templates at "web/template/user" and include them
  in the `YourApp.UserView`. For example, imagine we have the template:

      # web/templates/user/index.html.eex
      Hello <%= @name %>

  The `.eex` extension is called a template engine which tells Phoenix how
  to compile the code in the file into actual Elixir source code. After compiled,
  the template can be rendered as:

      Phoenix.View.render(YourApp.UserView, "index.html", name: "<NAME>")
      #=> {:safe, "Hello <NAME>"}

  We will discuss rendering with detail next.

  ## Rendering

  The main responsability of a view is to render a template.

  A template has a name, which is also contains a format. For example,
  in the previous section we have rendered the "index.html" template:

      Phoenix.View.render(YourApp.UserView, "index.html", name: "<NAME>")
      #=> {:safe, "Hello <NAME>"}

  When a view renders a template, the result returned is an inner
  representation specific to the template format. In the example above,
  we got: `{:safe, "Hello <NAME>"}`. The safe tuple annotates that our
  template is safe and that we don't need to escape its contents because
  all data was already encoded so far. Let's try to inject custom code:

      Phoenix.View.render(YourApp.UserView, "index.html", name: "John<br />Doe")
      #=> {:safe, "Hello John&lt;br /&gt;Doe"}

  This inner representation allows us to render and compose templates easily.
  For example, if you want to render JSON data, we could do so by adding a
  "show.json" entry to `render/2` in our view:

      defmodule YourApp.UserView do
        use YourApp.View

        def render("user.json", %{user: user}) do
          %{name: user.name, address: user.address}
        end
      end

  Notice that, in order to render JSON data, we don't need to explicitly
  return a JSON string! Instead we just return data that is encodable to
  JSON.

  Both JSON and HTML formats will be encoded only when passing the data
  to the controller via the `render_to_iodata/3` function. The
  `render_to_iodata/3` uses the notion of format encoders to convert a
  particular format to its string/iodata representation.

  Phoenix ships with some template engines and format encoders, which
  can be further configured in the Phoenix application. You can read
  more about format encoders in `Phoenix.Template` documentation.
  """

  @doc false
  defmacro __using__(options) do
    if root = Keyword.get(options, :root) do
      quote do
        @view_root unquote(root)
        unquote(__base__())
      end
    else
      # TODO: Remove this message once codebases have been upgraded
      raise """
      You are using the old API for Phoenix.View.
      Here is the new view for your application:

          defmodule YOURAPP.View do
            use Phoenix.View, root: "web/templates"

            # Everything in this block is available runs in this
            # module and in other views that use MyApp.View
            using do
              # Import common functionality
              import YOURAPP.I18n
              import YOURAPP.Router.Helpers

              # Use Phoenix.HTML to import all HTML functions (forms, tags, etc)
              use Phoenix.HTML

              # Common aliases
              alias Phoenix.Controller.Flash
            end

            # Functions defined here are available to all other views/templates
          end

      Replace YOURAPP by your actual application module name.
      """
    end
  end

  @doc """
  Implements the `__using__/1` callback for this view.

  This macro expects a block that will be executed in the current
  module and on all modules that use it. For example, the following
  code:<commit_msg>fixes typos and updates main view code representations to the latest version<commit_after>defmodule Phoenix.View do
  @moduledoc """
  Defines the view layer of a Phoenix application.

  This module is used to define the application main view, which
  serves as the base for all other views and templates in the
  application.

  The view layer also contains conveniences for rendering templates,
  including support for layouts and encoders per format.

  ## Examples

  Phoenix defines the main view module at /web/view.ex:

      defmodule YourApp.View do
        use Phoenix.View, root: "web/templates"

        # Everything that is imported, aliased, or used in this block is available
        # in the rest of this module and in any other view module that uses it.
        using do
          # Import common functionality
          import YourApp.I18n
          import YourApp.Router.Helpers

          # Use Phoenix.HTML to import all HTML functions (forms, tags, etc)
          use Phoenix.HTML

          # Common aliases
          alias Phoenix.Controller.Flash
        end

        # Functions defined here are available to all other views/templates
      end

  We can use the main view module to define other view modules:

      defmodule YourApp.UserView do
        use YourApp.View
      end

  Because we have defined the template root to be "web/template", `Phoenix.View`
  will automatically load all templates at "web/template/user" and include them
  in the `YourApp.UserView`. For example, imagine we have the template:

      # web/templates/user/index.html.eex
      Hello <%= @name %>

  The `.eex` extension is called a template engine which tells Phoenix how
  to compile the code in the file into actual Elixir source code. After it is 
  compiled, the template can be rendered as:

      Phoenix.View.render(YourApp.UserView, "index.html", name: "<NAME>")
      #=> {:safe, "Hello <NAME>"}

  We will discuss rendering in detail next.

  ## Rendering

  The main responsibility of a view is to render a template.

  A template has a name, which also contains a format. For example,
  in the previous section we have rendered the "index.html" template:

      Phoenix.View.render(YourApp.UserView, "index.html", name: "<NAME>")
      #=> {:safe, "Hello <NAME>"}

  When a view renders a template, the result returned is an inner
  representation specific to the template format. In the example above,
  we got: `{:safe, "Hello <NAME>"}`. The safe tuple annotates that our
  template is safe and that we don't need to escape its contents because
  all data was already encoded so far. Let's try to inject custom code:

      Phoenix.View.render(YourApp.UserView, "index.html", name: "John<br />Doe")
      #=> {:safe, "Hello John&lt;br /&gt;Doe"}

  This inner representation allows us to render and compose templates easily.
  For example, if you want to render JSON data, we could do so by adding a
  "show.json" entry to `render/2` in our view:

      defmodule YourApp.UserView do
        use YourApp.View

        def render("show.json", %{user: user}) do
          %{name: user.name, address: user.address}
        end
      end

  Notice that in order to render JSON data, we don't need to explicitly
  return a JSON string! Instead, we just return data that is encodable to
  JSON.

  Both JSON and HTML formats will be encoded only when passing the data
  to the controller via the `render_to_iodata/3` function. The
  `render_to_iodata/3` uses the notion of format encoders to convert a
  particular format to its string/iodata representation.

  Phoenix ships with some template engines and format encoders, which
  can be further configured in the Phoenix application. You can read
  more about format encoders in `Phoenix.Template` documentation.
  """

  @doc false
  defmacro __using__(options) do
    if root = Keyword.get(options, :root) do
      quote do
        @view_root unquote(root)
        unquote(__base__())
      end
    else
      # TODO: Remove this message once codebases have been upgraded
      raise """
      You are using the old API for Phoenix.View.
      Here is the new view for your application:

          defmodule YOURAPP.View do
            use Phoenix.View, root: "web/templates"

            # Everything that is imported, aliased, or used in this block is available
            # in the rest of this module and in any other view module that uses it.
            using do
              # Import common functionality
              import YOURAPP.I18n
              import YOURAPP.Router.Helpers

              # Use Phoenix.HTML to import all HTML functions (forms, tags, etc)
              use Phoenix.HTML

              # Common aliases
              alias Phoenix.Controller.Flash
            end

            # Functions defined here are available to all other views/templates
          end

      Replace YOURAPP by your actual application module name.
      """
    end
  end

  @doc """
  Implements the `__using__/1` callback for this view.

  This macro expects a block that will be executed in the current
  module and on all modules that use it. For example, the following
  code:
{'fraction_non_alphanumeric': 0.06199861344953947, 'fraction_numerical': 0.0015846290977518075, 'mean_word_length': 2.658695652173913, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3784081', 'n_tokens_mistral': 2687, 'n_tokens_neox': 2588, 'n_words': 1264}