title: Smarty Dynamic Template Include
body: <p>Is there a way to dynamically set a file to include? When I try, I get an error message (see below). For example take this template:</p>
  
  <p><strong>/templates/main.tpl</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;My Title&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          {include file='$file'}
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And this PHP Code:</p>
  
  <pre><code>&lt;?php
  switch($page){
      case "page1":
           $file = "/templates/page1.tpl";
           break;
      case "page2":
           $file = "/templates/page2.tpl";
           break;
      case "page3":
           $file = "/templates/page3.tpl";
           break;
  }
  $smarty-&gt;assign("file", $file);
  $smarty-&gt;display("/templates/main.tpl");
  </code></pre>
  
  <p>Smarty then throws this error to me:</p>
  
  <blockquote>
    <p>Unable to load template file '$file' in '/templates/main.tpl'</p>
  </blockquote>
  
  <p>Is there a way to dynamically set the template to use at that location?</p>