<p>I am making a plugin where I have about 10 tabs at the top.  Depending on the admin users privileges they may see a subset of 1-5 or 6-10 and super admins can see all 10.  The issue I am running into is I built the logic to show appropriate tabs for the user based on their privilege and it works fine.  What I now want to do is include that into an include file so I don't have the 60 lines of code on each of the 10 pages in the plugin.</p>

<p>I know I can put it into the main layout template in _layouts/cp but my logic is checking some variables that are set on each page of the plugin.  So if I put it in there none of my variables are set yet so the tabs don't show as nothing will match.  Example _layouts/cp template.</p>

<pre><code>{% if user.superAdmin == "no" %}
  {% if color == "red" %}
    {% if usercolor != "red" %}
      {% redirect redirectUrl %}
    {% else %}
      {% set tabs = { 
       red: {label: "red", url: url('admin/pluginname')},
       darkred: {label: "dark red", url: url('admin/pluginname/darkred')}
       } %}
    {% endif %}
 {% else %}
    {% if usercolor != "blue" %}
       {% redirect redirectUrl %}
    {% else %}
      {% set tabs = { 
       blue: {label: "blue", url: url('admin/pluginname/blue')},
       darkblue: {label: "dark blue", url: url('admin/pluginname/darkblue')}
       } %}
    {% endif %}
 {% endif %}
{% else %}
      {% set tabs = { 
       red: {label: "red", url: url('admin/pluginname')},
       darkred: {label: "dark red", url: url('admin/pluginname/darkred')},
       blue: {label: "blue", url: url('admin/pluginname/blue')},
       darkblue: {label: "dark blue", url: url('admin/pluginname/darkblue')
       } %}
{% endif %}

{% set block('content') %}
</code></pre>

<p>In each template file I will set what color the user is associated to and it should give a different set of tabs.  Example of template file.</p>

<pre><code>{% set redirectUrl = "/admin/pluginname/darkblue" %}

{% set color = "darkred" %}

{% set usercolor = user.userColor %}

{% extends "_layouts/cp" %}

{% set selectedTab = "darkred" %}

{% set content %}

{% endset %}  
</code></pre>

<p>If I move all my tab logic in the layout file and put it just below the extends _layout/cp the tabs work fine but not when it is in the layouts file.  I am sure there is an easy fix that I am just missing something but any help would be appreciated.  Not the end of the world if I have to have the tab logic within each template file but not ideal for any future changes.</p>

<p>Thanks</p>

CC BY-SA 4.0
2018-06-05T17:50:51.423Z
Is there a space in your `{% end if %}` ?
Clark Nelson
CC BY-SA 4.0
2018-06-05T18:02:11.527Z
Yes there was but this is not my actual code.  Just wrote it up for the question.  Code I have works if I move it from layout file and put it into template file so i know there are no errors such as that making it not work.  I fixed the spacing error in the question.  Thanks
Chris
CC BY-SA 4.0
2018-06-06T18:17:53.063Z
Do you have a specific question? I'm a bit confused as to what you are looking for.
Clark Nelson
CC BY-SA 4.0
2018-06-06T20:28:44.750Z
My question is how can I include the tab data in the master layout file?  Currently when I do no tabs show up on any of the pages but if I move all of the code into each child template file the tabs show up fine.  I just don't like having the same 50-60 lines of redundant code in 10 files when they all share a same master parent which is where the code should live.
Chris
CC BY-SA 4.0
2018-06-07T16:30:34.107Z
i'm not very familiar with the CP layout files, but can you use twig macros?
Clark Nelson
<plugin-development><templating><control-panel>
Add CP Tab content into include file
# Add CP Tab content into include file

(Asked by: username_0 on 2018-06-05)

I am making a plugin where I have about 10 tabs at the top.  Depending on the admin users privileges they may see a subset of 1-5 or 6-10 and super admins can see all 10.  The issue I am running into is I built the logic to show appropriate tabs for the user based on their privilege and it works fine.  What I now want to do is include that into an include file so I don't have the 60 lines of code on each of the 10 pages in the plugin.
I know I can put it into the main layout template in _layouts/cp but my logic is checking some variables that are set on each page of the plugin.  So if I put it in there none of my variables are set yet so the tabs don't show as nothing will match.  Example _layouts/cp template.
{% if user.superAdmin == "no" %}
  {% if color == "red" %}
    {% if usercolor != "red" %}
      {% redirect redirectUrl %}
    {% else %}
      {% set tabs = { 
       red: {label: "red", url: url('admin/pluginname')},
       darkred: {label: "dark red", url: url('admin/pluginname/darkred')}
       } %}
    {% endif %}
 {% else %}
    {% if usercolor != "blue" %}
       {% redirect redirectUrl %}
    {% else %}
      {% set tabs = { 
       blue: {label: "blue", url: url('admin/pluginname/blue')},
       darkblue: {label: "dark blue", url: url('admin/pluginname/darkblue')}
       } %}
    {% endif %}
 {% endif %}
{% else %}
      {% set tabs = { 
       red: {label: "red", url: url('admin/pluginname')},
       darkred: {label: "dark red", url: url('admin/pluginname/darkred')},
       blue: {label: "blue", url: url('admin/pluginname/blue')},
       darkblue: {label: "dark blue", url: url('admin/pluginname/darkblue')
       } %}
{% endif %}

{% set block('content') %}

In each template file I will set what color the user is associated to and it should give a different set of tabs.  Example of template file.
{% set redirectUrl = "/admin/pluginname/darkblue" %}

{% set color = "darkred" %}

{% set usercolor = user.userColor %}

{% extends "_layouts/cp" %}

{% set selectedTab = "darkred" %}

{% set content %}

{% endset %}  

If I move all my tab logic in the layout file and put it just below the extends _layout/cp the tabs work fine but not when it is in the layouts file.  I am sure there is an easy fix that I am just missing something but any help would be appreciated.  Not the end of the world if I have to have the tab logic within each template file but not ideal for any future changes.
Thanks

**Tags:** plugin-development, templating, control-panel
> **Comments:**
> **username_1**: Is there a space in your `{% end if %}` ?  
> **username_0**: Yes there was but this is not my actual code.  Just wrote it up for the question.  Code I have works if I move it from layout file and put it into template file so i know there are no errors such as that making it not work.  I fixed the spacing error in the question.  Thanks  
> **username_1**: Do you have a specific question? I'm a bit confused as to what you are looking for.  
> **username_0**: My question is how can I include the tab data in the master layout file?  Currently when I do no tabs show up on any of the pages but if I move all of the code into each child template file the tabs show up fine.  I just don't like having the same 50-60 lines of redundant code in 10 files when they all share a same master parent which is where the code should live.  
> **username_1**: i'm not very familiar with the CP layout files, but can you use twig macros?  

---

## 0 answers

