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

Re: [Cheetahtemplate-discuss] Template object caching in webware

Expand Messages
  • Mike Orr
    ... Actually, you d have to do it slightly differently than this. The code below is suitable for creating a template from a *.tmpl file directly (assuming
    Message 1 of 4 , Nov 5, 2001
    • 0 Attachment
      On Mon, Nov 05, 2001 at 08:38:01AM -0800, Mike Orr wrote:
      > On Mon, Nov 05, 2001 at 09:48:40AM -0500, Aaron Held wrote:
      > > In every servlet I currently have something like
      > >
      > > tf='ViewUser.ctd'
      > > templateFile=os.path.join(os.path.dirname(self.serverSidePath()),'templates',tf)
      > > t = Template(file=templateFile,searchList=[{'user':userToView}])
      > > self.writeln(t)
      > >
      > > So I guess that my template object is recreated from the file each time.
      > >
      > > Is there any way to create a template in a module and then reuse it with a different namelist?
      > >
      > > Something like=
      > > tf=LoadedTemplatesModules.ViewUser
      > > t = Template(tf,searchList=[{'user':userToView}])
      > > self.writeln(t)
      >
      > First I'll describe the general way, and then something that might be
      > more applicable to your situation. Generally:
      > 1) Place all your templates in *.tmpl files
      > 2) Run 'cheetah-compile' on them all. This creates same-name modules *.py .
      > 3) Request the *.py files on the web. Cheetah and Webware will take
      > care of caching and reusing the servlets and templates.
      >
      > For your situation, you could try a template cache dictionary. Create a
      > module with this code, or put it in an existing application module:

      Actually, you'd have to do it slightly differently than this. The code
      below is suitable for creating a template from a *.tmpl file directly
      (assuming constructTemplatePath() returns the path with ".tmpl"
      extension).

      It may be easier in your case to just to it this way and skip the
      cheetah-compile step. To actually use compiled *.py templates, you'd
      somehow have to put the uri directory in sys.path (if you have
      templates in multiple directories), import it, create a template object
      using the dynamic module name and class name appropriate to this
      request, worry about same-name templates in different directories which
      Python will refuse to "reimport" (reload() might have undesired side
      effects), and then *finally* you'll have your template object to store
      in the pool.

      > # poolModule.py
      > _pool = {}
      > def getTemplate(uri):
      > try:
      > return _pool[uri]
      > except KeyError:
      > path = constructTemplatePath(uri)
      > t = Template(file=path)
      > _pool[uri] = t
      >
      > Then in your servlets:
      >
      > import poolModule
      > t = poolModule.getTemplate(uri)
      > t.user = userToView
      > self.writeln(t)
      >
      > Of course, the last two statements are thread-unsafe because another
      > thread might change t.user between them. If simultaneous
      > use is a possibility, you'll have to give each template a mutex, spawn
      > an additional template if one is in use, wait till that one's finished,
      > or have a separate pool for each thread.
      >
      > --
      > -Mike (Iron) Orr, iron@... (if mail problems: mso@...)
      > http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol
      >
      > _______________________________________________
      > Cheetahtemplate-discuss mailing list
      > Cheetahtemplate-discuss@...
      > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss

      --
      -Mike (Iron) Orr, iron@... (if mail problems: mso@...)
      http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol

      _______________________________________________
      Cheetahtemplate-discuss mailing list
      Cheetahtemplate-discuss@...
      https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss
    • Aaron Held
      Thanks, This points me in the right direction. I didn t realize that you could add to the seachlist by adding class attribs - ... I ll start to centralize the
      Message 2 of 4 , Nov 5, 2001
      • 0 Attachment
        Thanks,

        This points me in the right direction.

        I didn't realize that you could add to the seachlist by adding class attribs -
        > t = poolModule.getTemplate(uri)
        > t.user = userToView
        > self.writeln(t)

        I'll start to centralize the templates in a module, and get to pooling when I have some time.

        ----- Original Message -----
        From: "Mike Orr" <iron@...>
        To: "Cheetah Template List" <cheetahtemplate-discuss@...>
        Sent: Monday, November 05, 2001 11:38 AM
        Subject: Re: [Cheetahtemplate-discuss] Template object caching in webware


        > On Mon, Nov 05, 2001 at 09:48:40AM -0500, Aaron Held wrote:
        > > In every servlet I currently have something like
        > >
        > > tf='ViewUser.ctd'
        > > templateFile=os.path.join(os.path.dirname(self.serverSidePath()),'templates',tf)
        > > t = Template(file=templateFile,searchList=[{'user':userToView}])
        > > self.writeln(t)
        > >
        > > So I guess that my template object is recreated from the file each time.
        > >
        > > Is there any way to create a template in a module and then reuse it with a different namelist?
        > >
        > > Something like=
        > > tf=LoadedTemplatesModules.ViewUser
        > > t = Template(tf,searchList=[{'user':userToView}])
        > > self.writeln(t)
        >
        > First I'll describe the general way, and then something that might be
        > more applicable to your situation. Generally:
        > 1) Place all your templates in *.tmpl files
        > 2) Run 'cheetah-compile' on them all. This creates same-name modules *.py .
        > 3) Request the *.py files on the web. Cheetah and Webware will take
        > care of caching and reusing the servlets and templates.
        >
        > For your situation, you could try a template cache dictionary. Create a
        > module with this code, or put it in an existing application module:
        >
        > # poolModule.py
        > _pool = {}
        > def getTemplate(uri):
        > try:
        > return _pool[uri]
        > except KeyError:
        > path = constructTemplatePath(uri)
        > t = Template(file=path)
        > _pool[uri] = t
        >
        > Then in your servlets:
        >
        > import poolModule
        > t = poolModule.getTemplate(uri)
        > t.user = userToView
        > self.writeln(t)
        >
        > Of course, the last two statements are thread-unsafe because another
        > thread might change t.user between them. If simultaneous
        > use is a possibility, you'll have to give each template a mutex, spawn
        > an additional template if one is in use, wait till that one's finished,
        > or have a separate pool for each thread.
        >
        > --
        > -Mike (Iron) Orr, iron@... (if mail problems: mso@...)
        > http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol
        >
        > _______________________________________________
        > Cheetahtemplate-discuss mailing list
        > Cheetahtemplate-discuss@...
        > https://lists.sourceforge.net/lists/listinfo/cheetahtemplate-discuss



        _______________________________________________
        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.