Differences

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

sourceresolver [2010/07/07 23:24]
81.187.95.96 created
sourceresolver [2016/02/10 22:39] (current)
24.114.56.119
Line 3: Line 3:
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 =====
If you don't use macros, you don't need ''SourceResolver'': If you don't use macros, you don't need ''SourceResolver'':
Line 9: Line 9:
     $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 =====
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'': 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'':
Line 19: Line 19:
             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 30:
     $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 ====
''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: ''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:
Line 52: Line 54:
    }     }
-PHPTAL will call ''getData()'' only when ''getRealPath()'' or ''getLastModifiedTime()'' return different values.+PHPTAL will call ''getData()'' only when ''getRealPath()'' or ''getLastModifiedTime()'' return different values.()