Added template map generator #3106

wants to merge 1 commit into


None yet
3 participants

Hounddog commented Nov 29, 2012

I have basically copied the classmap generator for this. Need to still clean it up but wanted to know if it was something required. I just think that if you have many templates it is tedious to write a template map manually for every template... Same as writing a classmap.

Module Config example

'view_manager' => array(
        'template_path_stack' => array(
            __DIR__ . '/../view',
        'template_map' => include __DIR__  . '../template_map.php',

To generate a classmap you can then just call php templatemap_generator.php from the root of your module and by default it would map all the templates within view.

I have also added the option to specify where the views are so you can also provide the library path aswell as the view path to generate the template map.

php templatemap_generator.php --view viewpath --librarypath modulepath

@weierophinney weierophinney added a commit that referenced this pull request Dec 11, 2012

@weierophinney weierophinney [#3106] Cleanup
- Was raising an exception of "view" directory did not exist; modified
  to show error message + usage message
- Removed import for ClassFileLocator
- Changed references from "classmap" to simply "map"
- Changed references from "autoload" to "map"

ThaDafinser commented Jul 8, 2013

@Hounddog shouldn't the "view" perfix be left away? Or is that intended?


// Generated by ZF2's ./bin/templatemap_generator.php
return array(
    'view/error/404'   => __DIR__ . '/view/error/404.phtml',
    'view/error/index' => __DIR__ . '/view/error/index.phtml',

Until yet i always used "error/404" and not "view/error/404" and i think the first one is more "common".
Maybe for BC compability just a parameter to include the "view" perfix or not?

If that's fine for you, maybe i can take also a look into it...shouldn't be much work.


ThaDafinser commented Jul 8, 2013

@Hounddog it's really easy (see gist:

Would it be okay for you? And suggestion for the parameter?


$rules = array(
    'help|h'      => 'Get usage message',
    'library|l-s' => 'Library to parse; if none provided, assumes current directory',
    'view|v-s'    => 'View path to parse; if none provided, assumes view as template directory',
    'useViewPath' => 'Use the view path to overwrite the key path mapping (instead of library path)',
    'output|o-s'  => 'Where to write map file; if not provided, assumes "template_map.php" in library directory',
    'append|a'    => 'Append to map file if it exists',
    'overwrite|w' => 'Whether or not to overwrite existing map file',

///.....after if (isset($opts->v)) { block

$replacePath = $libraryPath;
    $replacePath = $viewPath;

//more down
$map = new stdClass;
foreach ($l as $file) {
    if (!$file->isFile()) {
    $filename  = str_replace($replacePath . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $file->getFilename());


Hounddog commented Jul 8, 2013

It would be easier if you just created a pull request where we could view the changes :)


ThaDafinser commented Jul 8, 2013

Yes you are right....i'm currently at work, so i can't use git....don't ask why :-/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment