Labs/Bespin/ProjectTemplates: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
 
Line 16: Line 16:


  foo/
  foo/
     <nowiki>README-{{project.name}}.txt</nowiki>
     <nowiki>README-{project}.txt</nowiki>
     bar/
     bar/
         new.js
         new.js
Line 24: Line 24:
== Dynamic values ==
== Dynamic values ==


In the example above, you undoubtedly noticed the rather odd name of the README file: <tt>README-<nowiki>{{project.name}}</nowiki>.txt</tt>. When installing a template, the Python server uses the [http://jinja.pocoo.org/2/ Jinja2] template engine on both the filenames and the file contents.
In the example above, you undoubtedly noticed the rather odd name of the README file: <tt>README-<nowiki>{project}</nowiki>.txt</tt>. When installing a template, the Python server uses the [http://code.google.com/p/json-template/ JSON Template] template engine on both the filenames and the file contents.


So, if this template is installed into a project called "helloworld", you would see README-helloworld.txt at the top level of the project.
So, if this template is installed into a project called "helloworld", you would see README-helloworld.txt at the top level of the project.


The project object itself is available to the templates, which is how project.name is looked up.
By default, the following variables are available:
 
; project: the project name
; username: the username of the project's owner
; filename: the name of the file that is being generated
 
You can pass in additional values. Any undefined variables referenced in the template are simply replaced with an empty string.


== Web API ==
== Web API ==
Line 46: Line 52:
         the project, with the common root for the files chopped off.
         the project, with the common root for the files chopped off.
          
          
         Additionally, filenames containing { will be treated as Jinja2
         Additionally, filenames containing { will be treated as JSON Template
         templates and the contents of files will be treated as Jinja2
         templates and the contents of files will be treated as JSON Template
         templates. This means that the filenames can contain variables
         templates. This means that the filenames can contain variables
         that are substituted when the template is installed into the
         that are substituted when the template is installed into the
Line 53: Line 59:
         and small amounts of logic.
         and small amounts of logic.
          
          
         These Jinja2 templates automatically have "project" as a variable
         These JSON Template templates automatically have the following
         (you might often want to use project.name). You can pass in
        variables:
        a dictionary for other_vars and those values will also be available
       
        in the templates.
        * project: the project name
         * username: the project owner's username
        * filename: the name of the file being generated
       
        You can pass in a dictionary for other_vars and those values  
        will also be available in the templates.
         """
         """
canmove, Confirmed users, Bureaucrats and Sysops emeriti
1,093

edits

Navigation menu