Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-25T22:27:47.577Z
Answers.Text: I'm going to buy you lots of beers one day, @mmikkel.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-26T16:24:54.053Z
Answers.Text: Lol, I sincerely hope you get the chance to one day, @BradBell ;)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-09-25T19:57:38.267Z
Answers.LastActivityDate: 2015-09-25T19:57:38.267Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Tags: <templating>
Title: Shared Template
ThreadText: # 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) %}
          <div id="promos">
              <div class="promo_item">
                  <a href="">
                      <div class="promo_item_img">
                          {% for image in promo.promoImage %}
                              <img src="{{ image.url }}" width="222" height="101">
                          {% endfor %}
                      </div>
                  </a>
                  <a href="{promolink}"><h2 class="generic_title">| {{ promo.title }}</h2></a>
                  <p>{{ promo.promoTagline }}</p>
                          </div>
                      </div>
                  {% endfor %}
              {% endif %}
  
              {% if page == "blog" %}
                  {% for promo in craft.entries.section('promos').status('live').limit(5) %}
                      <div class="blog_item_inner blog_item_preorder">
                          <a href="">
                              <div class="blog_item_preorder_img">
                                  {% for image in promo.promoImage %}
                                      <img src="{{ image.url }}" width="222" height="101">
                                  {% endfor %}
                              </div>
                          </a>
                          <a href=""><h2 class="generic_title">| {{ promo.title }}</h2></a>
                          <p>{{ promo.promoTagline }}</p>
                      </div>
                  {% 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`](http://buildwithcraft.com/docs/templating/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" %}
           <div class="promo_item">
               <a href="">
                  <div class="promo_item_img">
                  {% for image in promo.promoImage %}
                      <img src="{{ image.url }}" width="222" height="101">
                  {% endfor %}
                  </div>
              </a>
              <a href="{promolink}"><h2 class="generic_title">| {{ promo.title }}</h2></a>
              <p>{{ promo.promoTagline }}</p>
          </div>
  
          {% case "blog" %}
          <div class="blog_item_inner blog_item_preorder">
              <a href="">
                  <div class="blog_item_preorder_img">
                  {% for image in promo.promoImage %}
                      <img src="{{ image.url }}" width="222" height="101">
                  {% endfor %}
                  </div>
              </a>
              <a href=""><h2 class="generic_title">| {{ promo.title }}</h2></a>
              <p>{{ promo.promoTagline }}</p>
          </div>
      {% endswitch %}
      {% endfor %}
  {% endif %}
  
  ```
  
  Finally, use the [`include`](http://twig.sensiolabs.org/doc/tags/include.html) 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).
  
  ```
  <div id="promos">
      {% include '_partials/promos' with { page : "homepage" } only %}
  </div>
  
  ```
  
  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](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself), 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 %}
           <div class="{{ classNames.item }}">
               <a href="">
                  <div class="{{ classNames.image }}">
                  {% for image in promo.promoImage %}
                      <img src="{{ image.url }}" width="222" height="101">
                  {% endfor %}
                  </div>
              </a>
              <a href="{promolink}"><h2 class="generic_title">| {{ promo.title }}</h2></a>
              <p>{{ promo.promoTagline }}</p>
          </div>
      {% 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, @username_2 ;)  
  
  ---
  
  