# Set $scripts variable for layout in AppController and SpecificController

**URL:** <https://discourse.cakephp.org/t/set-scripts-variable-for-layout-in-appcontroller-and-specificcontroller/5105>\
**Category:** Need Help\
**Created:** [October 16, 2018, 3:00pm UTC](https://discourse.cakephp.org/t/set-scripts-variable-for-layout-in-appcontroller-and-specificcontroller/5105 "2018-10-16T15:00:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mesiarm](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@mesiarm](https://discourse.cakephp.org/u/mesiarm)\
**Post date:** [October 16, 2018, 3:00pm UTC](https://discourse.cakephp.org/t/set-scripts-variable-for-layout-in-appcontroller-and-specificcontroller/5105/1 "2018-10-16T15:00:55Z")

</div>

I need to set google analytics script for layout on all pages, except one. So I call

> $scripts = $this-\>Script-\>getUniversalScript();  
> $this-\>set(compact(‘scripts’));

in AppController beforeFilter

And

> $scripts = $this-\>Script-\>getSpecificScript();  
> $this-\>set(compact(‘scripts’));

in SpecificController action, where I need specific google analytics script. I also have specific logic there so I cannot do it in AppController by taking controller name and action name. But it doesn´t work, because set() method in controller always takes value, which was first set (in AppController) and I need take last set (in SpecificController). Is there any way to make it? Thank you for any advice.

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [October 17, 2018, 3:50am UTC](https://discourse.cakephp.org/t/set-scripts-variable-for-layout-in-appcontroller-and-specificcontroller/5105/2 "2018-10-17T03:50:43Z")

</div>

You want to append instead of overwrite?

you can use `$this->_viewVars['scripts']` to read the value set in AppController beforeFilter and work with it

---

<div class="post-metadata">

**Author:** ![dakota](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dakota/32/1421_2.png) [@dakota](https://discourse.cakephp.org/u/dakota)\
**Post date:** [October 17, 2018, 12:20pm UTC](https://discourse.cakephp.org/t/set-scripts-variable-for-layout-in-appcontroller-and-specificcontroller/5105/3 "2018-10-17T12:20:50Z")

</div>

This kind of thing should be in your `AppView` class.

---

<div class="post-metadata">

**Author:** ![mesiarm](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@mesiarm](https://discourse.cakephp.org/u/mesiarm)\
**Post date:** [October 17, 2018, 2:03pm UTC](https://discourse.cakephp.org/t/set-scripts-variable-for-layout-in-appcontroller-and-specificcontroller/5105/4 "2018-10-17T14:03:05Z")

</div>

I forgot to mention, I´m using CakePHP 2.x which doesn´t support AppView

---

<div class="post-metadata">

**Author:** ![mesiarm](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@mesiarm](https://discourse.cakephp.org/u/mesiarm)\
**Post date:** [October 17, 2018, 2:05pm UTC](https://discourse.cakephp.org/t/set-scripts-variable-for-layout-in-appcontroller-and-specificcontroller/5105/5 "2018-10-17T14:05:52Z")

</div>

I tried to set universal scripts in beforeRender of AppController only if specific scripts are not set in $this-\>viewVars. But it still shows only universal scripts.
