{"id":13507829,"url":"https://github.com/khusnetdinov/phoenix_gon","last_synced_at":"2025-04-06T02:09:51.185Z","repository":{"id":20298196,"uuid":"89376117","full_name":"khusnetdinov/phoenix_gon","owner":"khusnetdinov","description":":fire: Phoenix variables in your JavaScript without headache.","archived":false,"fork":false,"pushed_at":"2023-10-10T17:00:56.000Z","size":54,"stargazers_count":99,"open_issues_count":1,"forks_count":21,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-09T13:08:04.084Z","etag":null,"topics":["controller-variables","environment-variables","hex","javascript","phoenix","phoenix-env","phoenix-variables"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/khusnetdinov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-04-25T15:21:37.000Z","updated_at":"2024-08-02T19:01:17.000Z","dependencies_parsed_at":"2023-11-14T03:44:31.998Z","dependency_job_id":null,"html_url":"https://github.com/khusnetdinov/phoenix_gon","commit_stats":{"total_commits":39,"total_committers":12,"mean_commits":3.25,"dds":0.5384615384615384,"last_synced_commit":"d2c4bf9f70c8537491e91b5fb7fa6c88b64961c7"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khusnetdinov%2Fphoenix_gon","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khusnetdinov%2Fphoenix_gon/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khusnetdinov%2Fphoenix_gon/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khusnetdinov%2Fphoenix_gon/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/khusnetdinov","download_url":"https://codeload.github.com/khusnetdinov/phoenix_gon/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247423515,"owners_count":20936626,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["controller-variables","environment-variables","hex","javascript","phoenix","phoenix-env","phoenix-variables"],"created_at":"2024-08-01T02:00:40.368Z","updated_at":"2025-04-06T02:09:51.166Z","avatar_url":"https://github.com/khusnetdinov.png","language":"Elixir","funding_links":[],"categories":["ECMAScript"],"sub_categories":[],"readme":"# PhoenixGon [![Hex.pm](https://img.shields.io/hexpm/v/plug.svg)](https://hex.pm/packages/phoenix_gon) [![Build Status](https://travis-ci.org/khusnetdinov/phoenix_gon.svg?branch=master)](https://travis-ci.org/khusnetdinov/phoenix_gon) [![Open Source Helpers](https://www.codetriage.com/khusnetdinov/phoenix_gon/badges/users.svg)](https://www.codetriage.com/khusnetdinov/phoenix_gon)\n\n## Your Phoenix variables in your JavaScript.\n\n![img](http://res.cloudinary.com/dtoqqxqjv/image/upload/v1492849051/github/gon.png)\n\n## Installation\n\nThe package can be installed by adding `phoenix_gon` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [{:phoenix_gon, \"~\u003e 0.4.0\"}]\nend\n```\n\n## Usage\n\n### Three steps configuration:\n\n1. You need add plug to `lib/project/router.ex` after plug `:fetch_session`.\n\n```elixir\ndefmodule Project.Router do\n  # ...\n\n  pipeline :browser do\n    # ...\n\n    plug :fetch_session\n    plug PhoenixGon.Pipeline\n\n    # ...\n  end\n\n  # ...\nend\n```\n\nPlug accepts options:\n\n    - `:env` - this option for hard overloading Mix.env.\n    - `:namespace` - namespace for javascript object in global window space.\n    - `:assets` - map for keeping permanent variables in javascript.\n    - `:camel_case` - if set to true, all assets names will be converted to camel case format on render.\n\n2. Add possibility to use view helper by adding `use PhoenixGon.View` in templates in `web/views/layout_view.ex` file:\n\n```elixir\ndefmodule Project.LayoutView do\n  # ...\n\n  import PhoenixGon.View\n\n  # ...\nend\n\n```\n\n3. Add helper `render_gon_script` to you layout in `/web/templates/layout/app.html.eex` before main javascript file:\n\n```elixir\n\n  # ...\n\n  \u003c%= render_gon_script(@conn) %\u003e\n  \u003cscript src=\"\u003c%= static_path(@conn, \"/js/app.js\") %\u003e\"\u003e\u003c/script\u003e\n\u003c/body\u003e\n```\n\nNow you can read phoenix variables in browser console and javascript code.\n\n### Phoenix controllers\n\nFor using gon in controllers just add:\n\n```elixir\ndefmodule Project.Controller do\n  # ...\n\n  import PhoenixGon.Controller\n\n  # ...\nend\n```\n\n#### Controller methods:\n\nAll controller variables are kept in `assets` map.\n\n- `put_gon` - Put variable to assets.\n- `update_gon` - Update variable in assets.\n- `drop_gon` - Drop variable in assets.\n- `get_gon` - Get variable from assets.\n\nExample:\n\n```elixir\ndef index(conn, _params) do\n  conn = put_gon(conn, controller: variable)\n  render conn, \"index.html\"\nend\n```\n\n```elixir\ndef index(conn, _params) do\n  conn = put_gon(conn, controller: variable)\n  redirect conn, to: \"/somewhere.html\"\nend\n```\n\n### JavaScript\n\nGon object is kept in `window`.\n\n#### Browser\n\nNow you can access to you variables in console:\n\n```javascript\n// browser console\n\nGon.assets()\n\n// Object {controller: \"variable\"}\n```\n\n#### JavaScript assets\n\n```JavaScript\n// Somewhere in javascript modules\n\nwindow.Gon.assets()\n\n```\n\n#### JavaScript methods:\n\nPhoenix env methods:\n\n- `getEnv()` - Returns current phoenix env.\n- `isDev()` - Returns boolean if development env.\n- `isProd()` - Returns boolean if production env.\n- `isCustomEnv(env)` - Return bollean if custom env.\n\nAssets variables methods:\n\n- `assets()` - Returns all variables setting in config and controllers.\n- `getAsset(key)` - Returns variable by key.\n\n### JSON Library\n\nPer default the `Jason` is used to encode JSON data, however this can be changed via the application configuration, eg:\n\n```elixir\nconfig :phoenix_gon, :json_library, Poison\n```\n\n## Contributors\n\nSpecial thanks to Andrey Soshchenko @getux.\n\n## License\n\nThe library is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhusnetdinov%2Fphoenix_gon","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhusnetdinov%2Fphoenix_gon","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhusnetdinov%2Fphoenix_gon/lists"}