Answers.Body: <p>One way I've come up with is to add the override path to the mix, and it will pick up the tpl file in the path.</p>
  
  <pre><code>$displayContactSummary = CRM_Utils_Request::retrieve('value', 'Boolean');
  
  $template = CRM_Core_Smarty::singleton();
  $template-&gt;clear_compiled_tpl();
  
  if ($displayContactSummary) {
  
      $templatePath = realpath(dirname(__FILE__) . "/templates_override");
      $template-&gt;addTemplateDir($templatePath);
  }
  </code></pre>
  
  <p>Clearing the cache every time though isn't ideal, otherwise when value=false, or isn't present, the old file doesn't load</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-02T12:24:35.053Z
Answers.LastActivityDate: 2016-04-02T12:24:35.053Z
Answers.UserDisplayName: Adam
Answers.Body: <p><strong>Use an extension's <code>templates</code> dir</strong></p>
  
  <p>Creating a simple extension via <a href="https://github.com/totten/civix" rel="nofollow"><code>civix</code></a> provides a <code>templates/</code> directory in the extension. This is searched before the default templates, so you can intercept and provide your own.</p>
  
  <p>If that doesn't go far enough &amp; you want to dynamically select a template (eg <em>replace Form.php, but only when foo=123</em>), these options are available:</p>
  
  <p><strong>Use CiviCRM's <a href="https://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterTemplateFile" rel="nofollow"><code>hook_civicrm_alterTemplateFile()</code></a></strong></p>
  
  <p>Alters template files, based on the form &amp; form data. This is something you'd do in an extension or CMS module. (Function signature refers to <code>$form</code>, I'm not sure what this will mean if you're rendering a page.)</p>
  
  <p><strong>Nest templates</strong></p>
  
  <p>Copy the default template <code>Foo.tpl</code> to your custom (or extension) template dir as <code>Foo-default.tpl</code> and <code>Foo-123.tpl</code>, create a wrapper <code>Foo.tpl</code> which uses Smarty to insert the correct template based on your criteria.</p>
  
  <p>This last method can be done without creating a custom extension.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-02T19:44:20.610Z
Answers.Text: Here's an extension which selects templates based on various criteria: https://github.com/fuzionnz/nz.co.fuzion.tplbytype
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-02T23:24:30.800Z
Answers.Text: Thank you for the sample code. Perfect. Also, with the smarty compiled TPL files, what is the naming convention to remove them if required? It seems they have the name, with a cache ID attached when dumping the smarty object.
Answers.UserDisplayName: Adam
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-02T23:47:16.890Z
Answers.Text: Clear the CiviCRM cache, *Administer > System Settings > Cleanup Caches*. (Yes, the filenames are predictable, but don't bother OR use `find . -name '*blah*' -exec rm -v {} \;` if you really want to.)
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-03T07:29:51.933Z
Answers.Text: There is a method: $template->clear_cache($item) or similar, which should be able to clear the individual item. Just not sure what $item is.
Answers.UserDisplayName: Adam
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-03T19:37:41.510Z
Answers.Text: [Smarty `clear_cache()` docs](http://www.smarty.net/docsv2/en/api.clear.cache.tpl) help? That is good to know about for developing. If [`&directoryCleanup=2`](https://wiki.civicrm.org/confluence/display/CRMDOC/Debugging+for+developers) is appended to the URL, it flushes the entire cache on each request.
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-03T23:11:48.390Z
Answers.Text: Yes the clear cache works as a whole, but I really only wanted to clear the single generated TPL file for that file.. rather than the brute force approach of clearing all cache.
Answers.UserDisplayName: Adam
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-03T23:24:02.443Z
Answers.Text: I don't know. When I have to deal with Smarty, my eyes mist up all red with the bloodrage pretty quick :) ... you might get an informed answer if you post a separate question on it?
Answers.UserDisplayName: Chris Burgess
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-04T01:10:30.390Z
Answers.Text: If I find out I'll post. From the objects I saw, they had weird names with a GUID attached to them. Thanks for the code above - Can dissect and use.
Answers.UserDisplayName: Adam
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-02T19:40:19.060Z
Answers.LastActivityDate: 2016-04-02T19:40:19.060Z
Answers.UserDisplayName: Chris Burgess
Body: <p>Is it possible to dynamically override the .tpl file path in CiviCRM?  Rather than use the corresponding TPL file to the Class.</p>
  
Tags: <smarty>
Title: Override TPL path dynamically
ThreadText: # Override TPL path dynamically
  
  (Asked by: username_0 on 2016-04-02)
  
  Is it possible to dynamically override the .tpl file path in CiviCRM? Rather than use the corresponding TPL file to the Class.
  
  **Tags:** smarty
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-04-02. Score: 4 | Accepted answer
  
  **Use an extension's `templates` dir**
  
  Creating a simple extension via [`civix`](https://github.com/totten/civix) provides a `templates/` directory in the extension. This is searched before the default templates, so you can intercept and provide your own.
  
  If that doesn't go far enough & you want to dynamically select a template (eg *replace Form.php, but only when foo=123*), these options are available:
  
  **Use CiviCRM's [`hook_civicrm_alterTemplateFile()`](https://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterTemplateFile)**
  
  Alters template files, based on the form & form data. This is something you'd do in an extension or CMS module. (Function signature refers to `$form`, I'm not sure what this will mean if you're rendering a page.)
  
  **Nest templates**
  
  Copy the default template `Foo.tpl` to your custom (or extension) template dir as `Foo-default.tpl` and `Foo-123.tpl`, create a wrapper `Foo.tpl` which uses Smarty to insert the correct template based on your criteria.
  
  This last method can be done without creating a custom extension.
  
  > **Comments:**
  > **username_1**: Here's an extension which selects templates based on various criteria: https://github.com/fuzionnz/nz.co.fuzion.tplbytype  
  > **username_0**: Thank you for the sample code. Perfect. Also, with the smarty compiled TPL files, what is the naming convention to remove them if required? It seems they have the name, with a cache ID attached when dumping the smarty object.  
  > **username_1**: Clear the CiviCRM cache, *Administer > System Settings > Cleanup Caches*. (Yes, the filenames are predictable, but don't bother OR use `find . -name '*blah*' -exec rm -v {} \;` if you really want to.)  
  > **username_0**: There is a method: $template->clear_cache($item) or similar, which should be able to clear the individual item. Just not sure what $item is.  
  > **username_1**: [Smarty `clear_cache()` docs](http://www.smarty.net/docsv2/en/api.clear.cache.tpl) help? That is good to know about for developing. If [`&directoryCleanup=2`](https://wiki.civicrm.org/confluence/display/CRMDOC/Debugging+for+developers) is appended to the URL, it flushes the entire cache on each request.  
  > **username_0**: Yes the clear cache works as a whole, but I really only wanted to clear the single generated TPL file for that file.. rather than the brute force approach of clearing all cache.  
  > **username_1**: I don't know. When I have to deal with Smarty, my eyes mist up all red with the bloodrage pretty quick :) ... you might get an informed answer if you post a separate question on it?  
  > **username_0**: If I find out I'll post. From the objects I saw, they had weird names with a GUID attached to them. Thanks for the code above - Can dissect and use.  
  
  ---
  
  ### Answer by username_0 on 2016-04-02. Score: 2
  
  One way I've come up with is to add the override path to the mix, and it will pick up the tpl file in the path.
  
  ```
  $displayContactSummary = CRM_Utils_Request::retrieve('value', 'Boolean');
  
  $template = CRM_Core_Smarty::singleton();
  $template->clear_compiled_tpl();
  
  if ($displayContactSummary) {
  
      $templatePath = realpath(dirname(__FILE__) . "/templates_override");
      $template->addTemplateDir($templatePath);
  }
  
  ```
  
  Clearing the cache every time though isn't ideal, otherwise when value=false, or isn't present, the old file doesn't load
  
  
  ---
  
  