[pLog-svn] Smarty caching problem

Christoph Feddersen feddersen at herr-der-ringe-film.de
Tue Jan 31 19:19:55 GMT 2006


Mark Wu wrote:
> 1. Did you already solve the {dynamic} block problem ?? This problem is not
> easy to solve, you have to write a "smarty plugin" to pass the dynamic
> value, take a look at:
> http://www.phpinsider.com/smarty-forum/viewtopic.php?t=5095

I plan to do this in a second step. I haven't looked at the inheritances
yet. I'll need to find a proper way to register smarty plugins from
within a plugin. I think that isn't possible with the current
implementation of Template, TemplateService, CachedPlugin etc.

What I've working (with hacking BlogView and DefaultAction) is a dynamic
authentication plugin (plugins/unported/authentication). Only minor
changes have been necessary to get this working with 1.0.3.


My main template looks like this:
=================================
...
<div id="content" class="narrowcolumn">
  {dynamic}
  {foreach from=$posts item=post}
    {include file="$blogtemplate/post.template"}
{/foreach}
{/dynamic}
</div>
...

Added at the end of post.template
=================================
{assign var=poster value=$post->getUserInfo()}<br/>

Poster: {$authentication->getPosterName($poster)}<br/>
User: {$authentication->getUserName()}<br/>


> 2. I don't think use the isDynamic() solve this problem, it just let the
> user know the plugin is dynamic, but .... It still not solve the real
> problem. The plugin can not used insdie {dynamic} block

My above example is using a template inside a dynamic block, isn't it?
isDynamic() would not tell the user that the plugin is dynamic, it will
tell the blogview class and defaultaction class that the plugin is
dynamic. Both classes have to load dynamic plugins even if they display
a cached template. The current implementation doesn't load plugins at
all if a cached template is rendered.

> We have to implement the "dynamic" inside our plugin, not let user use the
> {dynamic} block to make the plugin dynamic ...

I agree to the first statement. But making a plugin dynamic requires
a) a "dynamic" implementation inside the plugin classes
b) a dynamic rendering of the output (acchieved with {dynamic} or a
smarty plugin

For example, you're reading and displaying $_SESSION values inside your
plugin classes. So this would be a very simply way to get something
"dynamic". Now you want to display the value inside the panel template
with something like
{$currentUser}.

The panel template will be cached and {$currentUser} will be substituted
with "Joe". All other users will also see "Joe" instead of their
username. So in this example, you'll have use
{dynamic}{$currentUser}{/dynamic} to get it working. This is similar to
my example 1.

Or do you want to change the plugin policy, so that plugins shouldn't
not put their template code into existing templates but use their own
templates which have to be included?

If so, I agree that it will cause problems if a plugin templates relies
on variables of it's parent template. In my example, the $post variable
is necessary for the plugin
The first question that comes to my mind: What's the benefit from
putting the plugin template-code into a separate template?

> There are two ways to solve this problem if we don't want the plugin
> developers to develop "smarty plugin":
I don't think that they have to develop smarty plugins. They only need
to define which objects/variables they need in their dynamic function
calls. It should be possible to register the necessary smarty plugin
functions in a generic way.

> 1. User javascriipt to pass the dynamic value to page (no matter use ajax,
> or from sessions/cookies). 
> => User use {$pluginxxx->showStats()} in templates. It will render as
> <script type="text/javascript" src="plugins/pluginxxx/stats.js">, and loads
> the statistics data to the front page.
> 2. Fetch the plugin template in non-cache parameter ( Kindly take a look at
> secret plugin). 
That's a simple solution if the data is available from the session or
requests params. But what about a situation like above, where you have
to iterate over several items?

I'm currently thinking of this scenario:
I want to write a plugin that displays the current online
(MSN/ICQ/whatever) status of the post author right behind his username.
No problem with disabled template caching.

With caching enabled:
I'll have to call my plugin right after the username in the
post.template. The template code is already cached, so I can
't use the $posts object from their. So my plugin needs to:
- get the blog-id
- determine which posts to get (how many per page, which page has been
requested
- grab the poster-id
- somehow grab the status information from MSN/ICQ
- display it
- increment an internal counter so that the plugin knows that the first
posts is processed and to continue with the next posts on the next call.

>From my point of view, this would make plugins very complicated and
error-prone. Using the {dynamic} block and/or smarty-plugins seems
easier to me.

Christoph




More information about the pLog-svn mailing list