Differences

This shows you the differences between two versions of the page.

sourceresolver [2010/07/07 23:25]
81.187.95.96
sourceresolver [2024/05/31 06:09] (current)
104.194.134.81 1
Line 1: Line 1:
-===== Source Resolver =====+====== Source Resolver ======
You can create fake filesystems for PHPTAL templates. This lets you load templates //and macros// from any source, e.g. a database. You can create fake filesystems for PHPTAL templates. This lets you load templates //and macros// from any source, e.g. a database.
 +
===== Simplest way ===== ===== Simplest way =====
Line 7: Line 8:
If you don't use macros, you don't need ''SourceResolver'': If you don't use macros, you don't need ''SourceResolver'':
-     $phptal->setSource(load_source_from_anywhere());  // No source resolver necessary+     $phptal->setSource(load_source_from_anywhere());  // No source resolver necessary
===== Implementing custom source ===== ===== Implementing custom source =====
Line 19: Line 20:
             if (looks_like_my_path($path)) {              if (looks_like_my_path($path)) {
                 $source = load_template_from($path);                  $source = load_template_from($path);
-                 return new PHPTAL_StringSource($path);+                 return new PHPTAL_StringSource($source);
             }              }
         }          }
     }      }
 +
 +:!: (in PHPTAL older than 1.2.2 you need ''new PHPTAL_StringSource($source, md5($source))'')
The ''resolve'' function should return instance of ''PHPTAL_Source'' (if it supports the path) or ''NULL''. You can either use ''PHPTAL_StringSource'' class, or implement your own. The ''resolve'' function should return instance of ''PHPTAL_Source'' (if it supports the path) or ''NULL''. You can either use ''PHPTAL_StringSource'' class, or implement your own.
Line 28: Line 31:
     $phptal->addSourceResolver(new MySourceResolver());      $phptal->addSourceResolver(new MySourceResolver());
-PHPTAL will call ''resolve()'' method whenever it needs to load a template set via ''setTemplate()'' or ''metal:use-macro''.+PHPTAL will call ''resolve()'' method whenever it needs to load a template set via ''setTemplate()'' or ''metal:use-macro''. ()
==== Lazy loading of templates ==== ==== Lazy loading of templates ====
Line 52: Line 55:
    }     }
-PHPTAL will call ''getData()'' only when ''getRealPath()'' or ''getLastModifiedTime()'' return different values.+PHPTAL will call ''getData()'' only when ''getRealPath()'' or ''getLastModifiedTime()'' return different values.()