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

Re: [Cheetahtemplate-discuss] Best practice: initializing variable placeholder

Expand Messages
  • Mike Orr
    ... This would be the most elegant. from Cheetah.Tools.RecursiveNull import RecursiveNull values = { a : Foo , b : Bar } namespaces = [values,
    Message 1 of 6 , Sep 12, 2006
    • 0 Attachment
      On 9/12/06, Shannon -jj Behrens <jjinux@...> wrote:
      > On 9/12/06, John Quigley <jquigley@...> wrote:
      > > Shannon:
      > >
      > > > How about:
      > > >
      > > > $dict.get("b", "")
      > >
      > > I guess the operative word in my inquiry was 'elegant.' For a template
      > > that has, say, one hundred $dict elements, the above would be unwieldy.
      > > I'm curious if there's some way, preferably from to logic/python side,
      > > to tell Cheetah to either initialize all variables to an empty string,
      > > or to not fail when a placeholder isn't initialized before template
      > > compilation.
      >
      > In the past, people have done this with output filters. I do believe
      > there is a performance penalty.

      This would be the most elegant.

      from Cheetah.Tools.RecursiveNull import RecursiveNull
      values = {'a': "Foo", 'b': "Bar"}
      namespaces = [values, RecursiveNull()]
      t = Template(file="Template.tmpl", namespaces=namespaces)
      print t.respond()

      Then in the template you can just use:
      $a
      $b

      If the value doesn't exist in the first dict, it'll fall back to the
      second, which returns "" for anything.

      You can also make a pseudo dict like this:
      class SuccessfulDict(dict):
      def __getattr__(self, key):
      self.get(key, "")

      If you want to avoid interference between your form values and other
      values, you could put the form in a mini-template and pass the result
      as a value to the larger template.

      There is no significant penalty for using filters. But a filter just
      converts None to "" etc; it can't trap missing values. Use an Error
      Catcher to catch NotFound errors, but this *does* have a performance
      penalty because Python is not the quickest language on the planet to
      create an exception object. Still, unless you're filling the template
      several times a second, you're unlikely to notice it. However, in
      general we recommend using error catchers only for debugging.

      By the way, in answer to the other guy's question of how to get a list
      of missing values, use Cheetah.ErrorCatcher.ListErrors, and then call
      the .listErrors() method after filling the template. I knew there was
      something like that somewhere (since I wrote it :) but I couldn't
      remember where. It returns a list of dicts, which will tell you the
      line number and time as well as the name. You'll have to convert the
      names to a set yourself to uniquify them.

      --
      Mike Orr <sluggoster@...>

      -------------------------------------------------------------------------
      Using Tomcat but need to do more? Need to support web services, security?
      Get stuff done quickly with pre-integrated technology to make your job easier
      Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
      http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
      _______________________________________________
      Cheetahtemplate-discuss mailing list
      Cheetahtemplate-discuss@...
      https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
    • John Quigley
      Everyone: Thanks very much for all the help, your advice has set me in the right direction. Regards John Quigley ... Using Tomcat but need to do more? Need to
      Message 2 of 6 , Sep 12, 2006
      • 0 Attachment
        Everyone:

        Thanks very much for all the help, your advice has set me in the right
        direction.

        Regards
        John Quigley

        -------------------------------------------------------------------------
        Using Tomcat but need to do more? Need to support web services, security?
        Get stuff done quickly with pre-integrated technology to make your job easier
        Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
        http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
        _______________________________________________
        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.