Friday, June 24, 2011

Re: Conditionally generated templates

Hello,

"Tim Roberts" wrote :
> I write php using an MVC framework, and I'd love to be able to
> conditionally generate some of my new file content.
>
> The main ones would be
> vim app/controllers/projects_controller.php
>
> Should produce:
>
> <?php
> class ProjectsController extends AppController {
>
> }

With mu-template, you'll need a template-file named ~/.vim/after/template/php.vim that contains
-------------- >% -----------
VimL:" hint: php class file for Tim Roberts' project
VimL: let s:value_start = '¡'
VimL: let s:value_end = s:value_start
VimL: let s:marker_open = '<+'
VimL: let s:marker_close = '+>'
VimL: let s:filename = s:path_from_root(expand('%:p'))
VimL: let s:parts=split(s:filename,'/')
VimL: let s:class=matchstr(s:parts[-1], '.*\ze\.')
VimL: let s:class=lh#dev#naming#to_upper_camel_case(s:class)
VimL: let s:base=join(s:parts[0:-2],'_')
VimL: let s:base=lh#dev#naming#to_upper_camel_case(s:base)
<?php class ¡s:class¡ extends ¡s:base¡ {
<++>
}
-------------- >% -----------
The file is best saved in utf-8 encoding.

mu-template [http://code.google.com/p/lh-vim/wiki/muTemplate] has several dependencies listed on the wiki (lh-vim-lib, and lh-brackets) (If you use vim-addon-manager, it will be managed for you automatically).

The template-file itself has two dependencies.
* First it requires b:sources_root to be set before you open the file. AFAIK, only my version local_vimrc can ensures this: http://code.google.com/p/lh-vim/source/browse/misc/trunk/plugin/local_vimrc.vim

* Then it requires lh-dev function: to_upper_camel_case()
Its definition is really simple (if you prefer not to install this undocumented plugin): http://code.google.com/p/lh-vim/source/browse/dev/trunk/autoload/lh/dev/naming.vim#56

HTH,

--
Luc Hermitte
http://lh-vim.googlecode.com/
http://hermitte.free.fr/vim/

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment