# Source Resolver You can create fake filesystems for PHPTAL templates. This lets you load templates *and macros* from any source, e.g. a database. ## Simplest way If you don't use macros, you don't need `SourceResolver`: ``` $phptal->setSource(load_source_from_anywhere()); // No source resolver necessary ``` ## Implementing custom source However, if you'd like to avoid fetching template source on every request (explained later), or would like to use macros that load additional templates, implement your own `SourceResolver`: ``` class MySourceResolver implements PHPTAL_SourceResolver { function resolve($path) { if (looks_like_my_path($path)) { $source = load_template_from($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. ``` $phptal->addSourceResolver(new MySourceResolver()); ``` PHPTAL will call `resolve()` method whenever it needs to load a template set via `setTemplate()` or `metal:use-macro`. () ### Lazy loading of templates `PHPTAL_SourceResolver::resolve()` method could return object that doesn't load template immediately, and instead just returns information needed to load compiled template from cache: ``` class MyLazyTemplate extends PHPTAL_Source { function getRealPath() { return 'fake:/my/template'; // doesn't have to be on disk } function getLastModifiedTime() { return mktime(0,0,0, 7,7,2010); // unix timestamp } function getData() { return load_the_template(); } } ``` PHPTAL will call `getData()` only when `getRealPath()` or `getLastModifiedTime()` return different values.()