HandlebarsLoaderInlineLoader

The inline loader.

Defined (1)

The class is defined in the following location(s).

/vendor/xamin/handlebars.php/src/Handlebars/Loader/InlineLoader.php  
  1. class InlineLoader implements Loader 
  2. protected $fileName; 
  3. protected $offset; 
  4. protected $templates; 
  5.  
  6. /** 
  7. * The InlineLoader requires a filename and offset to process templates. 
  8. * The magic constants `__FILE__` and `__COMPILER_HALT_OFFSET__` are usually 
  9. * perfectly suited to the job: 
  10. * $loader = new \Handlebars\Loader\InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__); 
  11. * Note that this only works if the loader is instantiated inside the same 
  12. * file as the inline templates. If the templates are located in another 
  13. * file, it would be necessary to manually specify the filename and offset. 
  14. * @param string $fileName The file to parse for inline templates 
  15. * @param int $offset A string offset for the start of the templates. 
  16. * This usually coincides with the `__halt_compiler` 
  17. * call, and the `__COMPILER_HALT_OFFSET__`. 
  18. */ 
  19. public function __construct($fileName, $offset) 
  20. if (!is_file($fileName)) { 
  21. throw new \InvalidArgumentException('InlineLoader expects a valid filename.'); 
  22.  
  23. if (!is_int($offset) || $offset < 0) { 
  24. throw new \InvalidArgumentException('InlineLoader expects a valid file offset.'); 
  25.  
  26. $this->fileName = $fileName; 
  27. $this->offset = $offset; 
  28.  
  29. /** 
  30. * Load a Template by name. 
  31. * @param string $name template name 
  32. * @return string Handlebars Template source 
  33. */ 
  34. public function load($name) 
  35. $this->loadTemplates(); 
  36.  
  37. if (!array_key_exists($name, $this->templates)) { 
  38. throw new \InvalidArgumentException("Template {$name} not found."); 
  39.  
  40. return $this->templates[$name]; 
  41.  
  42. /** 
  43. * Parse and load templates from the end of a source file. 
  44. * @return void 
  45. */ 
  46. protected function loadTemplates() 
  47. if (!is_null($this->templates)) { 
  48. return; 
  49.  
  50. $this->templates = array(); 
  51. $data = file_get_contents($this->fileName, false, null, $this->offset); 
  52. foreach (preg_split('/^@@(?= [\w\d\.]+$)/m', $data, -1) as $chunk) { 
  53. if (trim($chunk)) { 
  54. list($name, $content) = explode("\n", $chunk, 2); 
  55. $this->templates[trim($name)] = trim($content);