<p>First, create a sub folder in <code>/templates</code> called <code>_partials</code>. The underscore essentially "hides" the templates inside that folder from Craft, i.e. they won't get routed to URLs like a regular template; perfect for any template that isn't going to work on its own.</p>

<p>Inside <code>_partials</code>, create a template called <code>_promos.html</code> or the like. Your Twig code above would work as-is, though I'm making some opinionated improvements below (like setting the <code>promos</code> in one place, checking if we actually have some promos to print, and using the <a href="http://buildwithcraft.com/docs/templating/switch" rel="nofollow"><code>switch</code></a> tag instead of <code>if</code> for readability:</p>

<pre><code>{% set promos = craft.entries.section('promos').status('live').limit(5) %}
{% if page is defined and promos|length %}
    {% for promo in promos %}
    {% switch page %}

        {% case "homepage" %}
         &lt;div class="promo_item"&gt;
             &lt;a href=""&gt;
                &lt;div class="promo_item_img"&gt;
                {% for image in promo.promoImage %}
                    &lt;img src="{{ image.url }}" width="222" height="101"&gt;
                {% endfor %}
                &lt;/div&gt;
            &lt;/a&gt;
            &lt;a href="{promolink}"&gt;&lt;h2 class="generic_title"&gt;| {{ promo.title }}&lt;/h2&gt;&lt;/a&gt;
            &lt;p&gt;{{ promo.promoTagline }}&lt;/p&gt;
        &lt;/div&gt;

        {% case "blog" %}
        &lt;div class="blog_item_inner blog_item_preorder"&gt;
            &lt;a href=""&gt;
                &lt;div class="blog_item_preorder_img"&gt;
                {% for image in promo.promoImage %}
                    &lt;img src="{{ image.url }}" width="222" height="101"&gt;
                {% endfor %}
                &lt;/div&gt;
            &lt;/a&gt;
            &lt;a href=""&gt;&lt;h2 class="generic_title"&gt;| {{ promo.title }}&lt;/h2&gt;&lt;/a&gt;
            &lt;p&gt;{{ promo.promoTagline }}&lt;/p&gt;
        &lt;/div&gt;
    {% endswitch %}
    {% endfor %}
{% endif %}
</code></pre>

<p>Finally, use the <a href="http://twig.sensiolabs.org/doc/tags/include.html" rel="nofollow"><code>include</code></a> tag to include the partial in another template. Use the <code>with</code> parameter to set the <code>page</code> variable, and the <code>only</code> directive to make sure the partial can't access any of the data in the parent template (like the <code>entry</code> variable etc). This helps prevent data pollution and variable name collisions (if you need access to the <code>entry</code> variable, you can include it in the <code>with</code> object).</p>

<pre><code>&lt;div id="promos"&gt;
    {% include '_partials/promos' with { page : "homepage" } only %}
&lt;/div&gt;
</code></pre>

<p>For the blog, replace the <code>page</code> property with "blog".</p>

<p>Finally, as far as I can tell, the only difference between the two blocks of Twig are different classes for the containers (<code>blog_item</code> vs. <code>promo_item</code> etc). If possible, I'd consider refactoring your code to use the same classnames both "blog" and "homepage" – this would make your code more lean and <a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="nofollow">DRY</a>, and probably easier to maintain.</p>

<p>If you can't change the classnames, you could also make them dynamic and expose them to the <code>include</code> statement like this:</p>

<pre><code>{% include '_partials/promos' with { page : "blog", classNames : {
    'item' : 'blog_item_inner blog_item_preorder',
    'image' : 'blog_item_preorder_img'
} } only %}
</code></pre>

<p>...and then print those classnames inside the partial (which now won't require the <code>switch</code> tag or duplicated markup:</p>

<pre><code>{% set promos = craft.entries.section('promos').status('live').limit(5) %}
{% if classNames is defined and promos|length %}
    {% for promo in promos %}
         &lt;div class="{{ classNames.item }}"&gt;
             &lt;a href=""&gt;
                &lt;div class="{{ classNames.image }}"&gt;
                {% for image in promo.promoImage %}
                    &lt;img src="{{ image.url }}" width="222" height="101"&gt;
                {% endfor %}
                &lt;/div&gt;
            &lt;/a&gt;
            &lt;a href="{promolink}"&gt;&lt;h2 class="generic_title"&gt;| {{ promo.title }}&lt;/h2&gt;&lt;/a&gt;
            &lt;p&gt;{{ promo.promoTagline }}&lt;/p&gt;
        &lt;/div&gt;
    {% endfor %}
{% endif %}    
</code></pre>

CC BY-SA 3.0
2015-09-25T22:27:47.577Z
I'm going to buy you lots of beers one day, @mmikkel.
Brad Bell
CC BY-SA 3.0
2015-09-26T16:24:54.053Z
Lol, I sincerely hope you get the chance to one day, @BradBell ;)
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2015-09-25T19:57:38.267Z
2015-09-25T19:57:38.267Z
Mats Mikkel Rummelhoff
<p>I have the same embed in 2 sections of a site (homepage and blog). There is a slight html/css change but the data is the same. </p>

<p>Is there any way to do a shared partial template?</p>

<p>Would I be using a block? Should I look into using setting a global var? </p>

<p>This is similar to what I was thinking:</p>

<pre><code>{% if page == "homepage" %}
    {% for promo in craft.entries.section('promos').status('live').limit(5) %}
        &lt;div id="promos"&gt;
            &lt;div class="promo_item"&gt;
                &lt;a href=""&gt;
                    &lt;div class="promo_item_img"&gt;
                        {% for image in promo.promoImage %}
                            &lt;img src="{{ image.url }}" width="222" height="101"&gt;
                        {% endfor %}
                    &lt;/div&gt;
                &lt;/a&gt;
                &lt;a href="{promolink}"&gt;&lt;h2 class="generic_title"&gt;| {{ promo.title }}&lt;/h2&gt;&lt;/a&gt;
                &lt;p&gt;{{ promo.promoTagline }}&lt;/p&gt;
                        &lt;/div&gt;
                    &lt;/div&gt;
                {% endfor %}
            {% endif %}

            {% if page == "blog" %}
                {% for promo in craft.entries.section('promos').status('live').limit(5) %}
                    &lt;div class="blog_item_inner blog_item_preorder"&gt;
                        &lt;a href=""&gt;
                            &lt;div class="blog_item_preorder_img"&gt;
                                {% for image in promo.promoImage %}
                                    &lt;img src="{{ image.url }}" width="222" height="101"&gt;
                                {% endfor %}
                            &lt;/div&gt;
                        &lt;/a&gt;
                        &lt;a href=""&gt;&lt;h2 class="generic_title"&gt;| {{ promo.title }}&lt;/h2&gt;&lt;/a&gt;
                        &lt;p&gt;{{ promo.promoTagline }}&lt;/p&gt;
                    &lt;/div&gt;
                {% endfor %}
            {% endif %}
</code></pre>

<p>Just want to get the best solution in place early.</p>

<templating>
Shared Template
# Shared Template

(Asked by: username_0 on 2015-09-25)

I have the same embed in 2 sections of a site (homepage and blog). There is a slight html/css change but the data is the same. 
Is there any way to do a shared partial template?
Would I be using a block? Should I look into using setting a global var? 
This is similar to what I was thinking:
{% if page == "homepage" %}
    {% for promo in craft.entries.section('promos').status('live').limit(5) %}
        
            
                
                    
                        {% for image in promo.promoImage %}
                            
                        {% endfor %}
                    
                
                | {{ promo.title }}
                {{ promo.promoTagline }}
                        
                    
                {% endfor %}
            {% endif %}

            {% if page == "blog" %}
                {% for promo in craft.entries.section('promos').status('live').limit(5) %}
                    
                        
                            
                                {% for image in promo.promoImage %}
                                    
                                {% endfor %}
                            
                        
                        | {{ promo.title }}
                        {{ promo.promoTagline }}
                    
                {% endfor %}
            {% endif %}

Just want to get the best solution in place early.

**Tags:** templating

---

## 1 answers

### Answer by username_1 on 2015-09-25. Score: 4 | Accepted answer

First, create a sub folder in /templates called _partials. The underscore essentially "hides" the templates inside that folder from Craft, i.e. they won't get routed to URLs like a regular template; perfect for any template that isn't going to work on its own.
Inside _partials, create a template called _promos.html or the like. Your Twig code above would work as-is, though I'm making some opinionated improvements below (like setting the promos in one place, checking if we actually have some promos to print, and using the switch tag instead of if for readability:
{% set promos = craft.entries.section('promos').status('live').limit(5) %}
{% if page is defined and promos|length %}
    {% for promo in promos %}
    {% switch page %}

        {% case "homepage" %}
         
             
                
                {% for image in promo.promoImage %}
                    
                {% endfor %}
                
            
            | {{ promo.title }}
            {{ promo.promoTagline }}
        

        {% case "blog" %}
        
            
                
                {% for image in promo.promoImage %}
                    
                {% endfor %}
                
            
            | {{ promo.title }}
            {{ promo.promoTagline }}
        
    {% endswitch %}
    {% endfor %}
{% endif %}

Finally, use the include tag to include the partial in another template. Use the with parameter to set the page variable, and the only directive to make sure the partial can't access any of the data in the parent template (like the entry variable etc). This helps prevent data pollution and variable name collisions (if you need access to the entry variable, you can include it in the with object).

    {% include '_partials/promos' with { page : "homepage" } only %}


For the blog, replace the page property with "blog".
Finally, as far as I can tell, the only difference between the two blocks of Twig are different classes for the containers (blog_item vs. promo_item etc). If possible, I'd consider refactoring your code to use the same classnames both "blog" and "homepage" – this would make your code more lean and DRY, and probably easier to maintain.
If you can't change the classnames, you could also make them dynamic and expose them to the include statement like this:
{% include '_partials/promos' with { page : "blog", classNames : {
    'item' : 'blog_item_inner blog_item_preorder',
    'image' : 'blog_item_preorder_img'
} } only %}

...and then print those classnames inside the partial (which now won't require the switch tag or duplicated markup:
{% set promos = craft.entries.section('promos').status('live').limit(5) %}
{% if classNames is defined and promos|length %}
    {% for promo in promos %}
         
             
                
                {% for image in promo.promoImage %}
                    
                {% endfor %}
                
            
            | {{ promo.title }}
            {{ promo.promoTagline }}
        
    {% endfor %}
{% endif %}

> **Comments:**
> **username_2**: I'm going to buy you lots of beers one day, @mmikkel.  
> **username_1**: Lol, I sincerely hope you get the chance to one day, @BradBell ;)  

---

