# Global variable

**URL:** <https://discuss.emberjs.com/t/global-variable/16719>\
**Category:** Uncategorized\
**Created:** [June 27, 2019, 8:55am UTC](https://discuss.emberjs.com/t/global-variable/16719 "2019-06-27T08:55:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hari\_Priya](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/hari_priya/32/16545_2.png) [@Hari\_Priya](https://discuss.emberjs.com/u/Hari_Priya)\
**Post date:** [June 27, 2019, 8:55am UTC](https://discuss.emberjs.com/t/global-variable/16719/1 "2019-06-27T08:55:51Z")

</div>

How to set a variable so that it can be accessed from every template directly?

---

<div class="post-metadata">

**Author:** ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)\
**Post date:** [June 27, 2019, 2:14pm UTC](https://discuss.emberjs.com/t/global-variable/16719/2 "2019-06-27T14:14:28Z")

</div>

You have a couple options, which one you use depends on your use case. Off the top of my head you could…

1. Store the variable in a service and use an initializer to inject the service in every controller. This would only work with route templates…so you could also auto-inject the service into components as well if you wanted that

2. Store the variable in a service and write a helper which injects the service and simply returns the value

In general though the explicitness of data declaration and injection is encouraged so I wouldn’t do something like this unless it was necessary and had a very specific use case.

---

<div class="post-metadata">

**Author:** ![ef4](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ef4/32/13470_2.png) [@ef4](https://discuss.emberjs.com/u/ef4)\
**Post date:** [June 27, 2019, 2:38pm UTC](https://discuss.emberjs.com/t/global-variable/16719/3 "2019-06-27T14:38:10Z")

</div>

If you just want a hard-coded value, return it from a helper:

```nohighlight
//app/helpers/global-title.js
import { helper } from '@ember/component/helper';

export default helper(function globalTitle() {
  return "This is Our Global Title";
}));

```

```nohighlight
{{! use it like this:}}
<div>{{our-global-title}}</div>

{{! or like this: }}
<SomeComponent @title={{our-global-title}} />

```

If you need to change it over time, use @dknutsen’s suggestion of a helper that injects a service. Services are the place to store shared global state.

---

<div class="post-metadata">

**Author:** ![Hari\_Priya](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/hari_priya/32/16545_2.png) [@Hari\_Priya](https://discuss.emberjs.com/u/Hari_Priya)\
**Post date:** [June 28, 2019, 7:23am UTC](https://discuss.emberjs.com/t/global-variable/16719/4 "2019-06-28T07:23:23Z")

</div>

Thank you for suggestions.That helps me a lot. I need to get the value of key from json object in template.The problem is the key contains “.” character in it.I am not able to access the value using object.key

---

<div class="post-metadata">

**Author:** ![CezaryH](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/cezaryh/32/13527_2.png) [@CezaryH](https://discuss.emberjs.com/u/CezaryH)\
**Post date:** [June 28, 2019, 11:33am UTC](https://discuss.emberjs.com/t/global-variable/16719/5 "2019-06-28T11:33:01Z")

</div>

you can use “get” helper. BTW you can store global variable in environment object, and access it thru helper

---

<div class="post-metadata">

**Author:** ![abdalshafiealmajdoup](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/abdalshafiealmajdoup/32/17585_2.png) [@abdalshafiealmajdoup](https://discuss.emberjs.com/u/abdalshafiealmajdoup)\
**Post date:** [September 12, 2021, 10:08am UTC](https://discuss.emberjs.com/t/global-variable/16719/6 "2021-09-12T10:08:29Z")

</div>

pls write simple example here …
