Loading ...
Sorry, an error occurred while loading the content.

[Cheetahtemplate-discuss] Getting at a list of names?

Expand Messages
  • Tracy S. Ruggles
    Hey all, Is there a way to get at the list of names defined in a template where a template like: ## ---SimpleTest.tmpl--- My name is $name. I am $age years
    Message 1 of 2 , Feb 21, 2003
    • 0 Attachment
      Hey all,

      Is there a way to get at the list of names defined in a template where
      a template like:

      ## ---SimpleTest.tmpl---
      My name is $name. I am $age years old. I love $favorites.rockStar!!!

      ... could be used like so:

      tsr% cheetah compile SimpleTest.tmpl
      Compiling SimpleTest.tmpl -> SimpleTest.py (backup SimpleTest.py_bak)
      tsr$ python
      >>> from SimpleTest import SimpleTest
      >>> print SimpleTest._nameList
      ['name', 'age', 'favorites.rockStar']

      This would be helpful for when designers are creating templates and we
      would need to know the variables they expect for the template and check
      it against the ones that will be given in the searchList before it is
      instantiated... giving them a chance to see what's missing.

      An alternate would be to allow a preview page that would dynamically
      add errorCatcher lines to the beginning of the template, pre-compile
      and display it for them so they can see what exactly won't be found
      when it goes to production. A la:

      def preview (template, **sampleData):
      data = open(template + '.tmpl', 'r').readlines()
      data = [
      '#from Cheetah.ErrorCatchers import Echo',
      '#errorCatcher Echo',
      ] + data
      f = open(template + '.tmpl', 'w')
      f.write ('\n'.join(data))
      f.write ('\n')
      f.close()
      os.system('cheetah compile %s.tmpl' % template)
      module = __import__(template, globals(), locals(), template)
      klass = getattr(module, template)
      return klass(searchList=[sampleData])

      def approve (template):
      data = open(template + '.tmpl', 'r').readlines()
      data = data[2:]
      f = open(template + '.tmpl', 'w')
      f.write('\n'.join(data))
      f.write('\n')
      f.close()
      os.system('cheetah compile %s.tmpl' % template)

      print preview('SimpleTest', name='Bob', age=30)
      if ok:
      approve('SimpleTest')

      Thanks,
      --Tracy



      -------------------------------------------------------
      This SF.net email is sponsored by: SlickEdit Inc. Develop an edge.
      The most comprehensive and flexible code editor you can use.
      Code faster. C/C++, C#, Java, HTML, XML, many more. FREE 30-Day Trial.
      www.slickedit.com/sourceforge
      _______________________________________________
      Cheetahtemplate-discuss mailing list
      Cheetahtemplate-discuss@...
      https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
    • Tavis Rudd
      Hi Tracy, I don t think there s anything at the moment, but it s certainly possible to extend the compiler to create a list of placeholder names. I ll add it
      Message 2 of 2 , Mar 2, 2003
      • 0 Attachment
        Hi Tracy,
        I don't think there's anything at the moment, but it's certainly possible to
        extend the compiler to create a list of placeholder names. I'll add it to
        our todo list.

        In the mean time you might want to use grep:

        grep \$[a-zA-Z_] myTemplate.tmpl

        Cheers,
        Tavis

        On Friday 21 February 2003 07:15, Tracy S.Ruggles wrote:
        > Hey all,
        >
        > Is there a way to get at the list of names defined in a template where
        > a template like:
        >
        > ## ---SimpleTest.tmpl---
        > My name is $name. I am $age years old. I love $favorites.rockStar!!!
        >
        > ... could be used like so:
        >
        > tsr% cheetah compile SimpleTest.tmpl
        > Compiling SimpleTest.tmpl -> SimpleTest.py (backup SimpleTest.py_bak)
        > tsr$ python
        >
        > >>> from SimpleTest import SimpleTest
        > >>> print SimpleTest._nameList
        >
        > ['name', 'age', 'favorites.rockStar']
        >
        > This would be helpful for when designers are creating templates and we
        > would need to know the variables they expect for the template and check
        > it against the ones that will be given in the searchList before it is
        > instantiated... giving them a chance to see what's missing.
        >
        > An alternate would be to allow a preview page that would dynamically
        > add errorCatcher lines to the beginning of the template, pre-compile
        > and display it for them so they can see what exactly won't be found
        > when it goes to production. A la:
        >
        > def preview (template, **sampleData):
        > data = open(template + '.tmpl', 'r').readlines()
        > data = [
        > '#from Cheetah.ErrorCatchers import Echo',
        > '#errorCatcher Echo',
        > ] + data
        > f = open(template + '.tmpl', 'w')
        > f.write ('\n'.join(data))
        > f.write ('\n')
        > f.close()
        > os.system('cheetah compile %s.tmpl' % template)
        > module = __import__(template, globals(), locals(), template)
        > klass = getattr(module, template)
        > return klass(searchList=[sampleData])
        >
        > def approve (template):
        > data = open(template + '.tmpl', 'r').readlines()
        > data = data[2:]
        > f = open(template + '.tmpl', 'w')
        > f.write('\n'.join(data))
        > f.write('\n')
        > f.close()
        > os.system('cheetah compile %s.tmpl' % template)
        >
        > print preview('SimpleTest', name='Bob', age=30)
        > if ok:
        > approve('SimpleTest')
        >
        > Thanks,
        > --Tracy
        >
        >
        >
        > -------------------------------------------------------
        > This SF.net email is sponsored by: SlickEdit Inc. Develop an edge.
        > The most comprehensive and flexible code editor you can use.
        > Code faster. C/C++, C#, Java, HTML, XML, many more. FREE 30-Day Trial.
        > www.slickedit.com/sourceforge
        > _______________________________________________
        > Cheetahtemplate-discuss mailing list
        > Cheetahtemplate-discuss@...
        > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss



        -------------------------------------------------------
        This sf.net email is sponsored by:ThinkGeek
        Welcome to geek heaven.
        http://thinkgeek.com/sf
        _______________________________________________
        Cheetahtemplate-discuss mailing list
        Cheetahtemplate-discuss@...
        https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
      Your message has been successfully submitted and would be delivered to recipients shortly.