wiki:ExtendingQuickstart
Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 7 (modified by milde, 13 years ago) (diff)

Cheetah note; removing inherited template files

With TurboGears 0.9 (or svn rev 106) quickstart now uses PasteScript and therefore supports user templates.

Why?

Why would you want to do this? Well, if you have your own conventions for applications, this is a way to add those to new TurboGears projects -- documentation templates, testing frameworks, whatever.

Also, if you already have a project started, you can run quickstart on it again (perhaps with your new template!) and see how the current project differs, and maybe apply some of the changes.

How?

So let's go ahead and create our own template!

Let's say that you have a default.css file that you always include in your webapp.

Let's start creating some files and dirs.

mytemplate/

mytemplate/setup.py

mytemplate/mytemplate/init.py

mytemplate/mytemplate/template/+package+/static/css/default.css

First we will edit the setup.py

from setuptools import setup, find_packages
from turbogears import finddata

setup(name='mytemplate',
      packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
      zip_safe=False,
      package_data=finddata.find_package_data(),
      entry_points="""
          [paste.paster_create_template]
              mytemplate = mytemplate:MyTemplate
        """
      )

This setup basically tells pastescript that there is a template called mytemplate and its class is MyTemplate? in the mytemplate module

Now let's edit __init__.py

from paste.script import templates
import pkg_resources

class MyTemplate(templates.Template):

    egg_plugins = ['MyTemplate']
    _template_dir = pkg_resources.resource_filename("mytemplate", "template")
    summary = "my own cute template"

Now all you have to do is add documentation templates, testing frameworks... to the template dir.

Now you can run: tg-admin quickstart -t mytemplate and there should be a default.css file in your static/css/ dir!

See  http://pythonpaste.org/script/developer.html#templates for more info

/elvelind grandin

Notes

If you're indeed extending quickstart and not creating a fresh template, you may want to add use_cheetah = True as an attribute of your template class, as quickstart makes use of Cheetah for IdentityManagement, though it might not be necessary in 0.8.

Advanced Topics

Removing Inherited Files

Sometimes you'll want to use a quickstart template, but perhaps you don't like one part. In this case your template's __init__.py might look like this:

from paste.script import templates
import pkg_resources
import os

class AdvancedStart(templates.Template):

    egg_plugins = ['AdvancedStart']
    _template_dir = pkg_resources.resource_filename('advancedstart', 'template')
    summary = 'TurboGears Advanced project template'
    required_templates = ['turbogears']
    use_cheetah = True

    def post(self, command, output_dir, vars):
        templates.Template.post(self, command, output_dir, vars)
        try:
            path = os.path.join(output_dir, vars['package'] + '/controllers.py')
            os.remove(path)
            print 'Removing', path
        except OSError:
            pass

Note: the above was made for use with TurboGears 0.9's quickstart template.