Skip to content

atomicptr/tmplr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tmplr

Quickly create new files from templates.

Usage

Assuming we are in an empty directory:

$ tmplr src/index.php

This will create the directory "src" and an empty index.php file because we have not created a template just yet.

Lets create one!

First lets go into our template directory:

$ cd $(tmplr --template-dir)

next lets create a template file for PHP classes and PHP enums

$ hx [class].php
---
name: PHP Class Template
vars:
  - name: namespace
    prompt: Namespace for this class
---
<?php

namespace {{.namespace}};

class {{.class}} {
    public function __constructor() {
    }
}

and...

$ hx [enum].php
---
name: PHP Enum Template
vars:
  - name: namespace
    prompt: Namespace for this class
---
<?php

namespace {{.namespace}};

enum {{.enum}} {
}

Short explanation of the template syntax:

There is a frontmatter where you can define a name and variables the user has to enter when they are creating the template. And after that there is the actual template that will be later parsed by Gos template/text templating engine.

lets get back to the project dir and create a new file:

$ cd ~/to/my/project-dir
$ tmplr src/controller/IndexController.php

Select Template

Set User Vars

See result

tmplr matches the filename against all files in its template directory in this case "IndexController.php" matches "[class].php" and "[enum].php"

The [...] part is variable and will as you can see above also be exported to the template.

You could also create a template like this "[controllerName]Controller.php" which would also match for "IndexController" to make even more precise templates

Meta template variables

tmplr also has some extra template variables that you don't have to specify like:

  • _cwd - The current working directory
  • _path - The absolute path of your new file, e.g. pkg/cli/test.go will use "/home/YOURNAME/dev/tmplr/pkg/cli/test.go" here
  • _dirname - The name of the directory the new file is in, e.g. pkg/cli/test.go will use "cli" here
  • _filename - The name of the file, e.g. pkg/cli/test.go will use "test.go" here

Name

tmplr comes from a mixture of template and Templar and is also supposed to be pronounced "Templar".

License

GNU General Public License v3