Page 1 of 2

smarty $view no longer available

Posted: Thu Jun 12, 2008 7:52 pm
by Don Chambers
I just noticed that logic I use in a template to determine the value of $view was no longer working.... when I set {debug} in entries.tpl, $view isn't listed as one of the smarty variables. This is with the latest 1.3 branch.

Doesn't appear to be my template either - also tried it with carl_contest - same thing - no $view.

Posted: Fri Jun 13, 2008 4:14 am
by Don Chambers
Found it.... the microformats plugin seems to be the problem. Its genpage?

Posted: Fri Jun 13, 2008 10:19 am
by garvinhicking
Hi!

You mean when you disable microformats, $view gets in again?

I don't see anything in the 'genpage' hook of microformats that would even touch this variable. You are using the recent version in spartacus, not some other version?

Regards,
Garvin

Posted: Fri Jun 13, 2008 4:56 pm
by Don Chambers
Yes Garvin - when I deactivate the microformats plugin - which I had recently installed out of curiosity - $view is again available.

Posted: Fri Jun 13, 2008 7:03 pm
by garvinhicking
Hi!

Hm, can you edit the plugin and insert a simple "return true;" just as the first line after the "case 'genpage':" case? This could show whether the genpage hook is responsible,or another one.

I'll be offline for the weekend, so I'll have to get backto you next week.

Regards,
Garvin

Posted: Sun Jun 15, 2008 2:39 pm
by mattsches
Don, let me know it it's got anything to do with the microformats plugin. I have begun to rewrite the plugin a couple of months ago, but never found the motivation to finish it :?

Now, after I received a copy of Garvin's excellent S9y book, I am determined to delve into the depths of my plugin again :wink: Right now, however, I'm busy tweaking some options in my blog that I've just found out exist. S9y book FTW!!

- Mattsches

Posted: Sun Jun 15, 2008 9:51 pm
by Don Chambers
garvinhicking wrote:Hi!

Hm, can you edit the plugin and insert a simple "return true;" just as the first line after the "case 'genpage':" case? This could show whether the genpage hook is responsible,or another one.

I'll be offline for the weekend, so I'll have to get backto you next week.

Regards,
Garvin


Garvin, at line 199 I added return true, and $view is again available.

Code: Select all

                case 'genpage':
                    return true;

Posted: Mon Jun 16, 2008 10:11 am
by garvinhicking
Hi!

And if you move that return true after the line where serendipity_smarty_init() is called? Maybe that function call somehow resets the view...

Regards,
Garvin

Posted: Mon Jun 16, 2008 4:21 pm
by Don Chambers
That is correct Garvin. If located after the smarty_init(), there is no $view.

Posted: Mon Jun 16, 2008 4:42 pm
by garvinhicking
Hi!

That is too strange, because the serendipity_smarty_init() call does not tamper with $serendipity['view'].

Is your template using a config.inc.php? That file will then be utilized, maybe that could be responsible. If you use it, please temporarily remove that config.inc.php file?

Regards,
Garvin

Posted: Mon Jun 16, 2008 6:05 pm
by Don Chambers
Before I even posted this thread, I switched my template to carl_contest.... the behavior is the same there.

Posted: Mon Jun 16, 2008 8:12 pm
by judebert
Yup, the microformats plugin is causing it.

The problem starts in include/genpage.inc.php, line 221. First we call the genpage hook, then we call serendipity_smarty_init(). This is where $view gets set; it's among the variables passed to serendipity_smarty_init() to be assigned to Smarty.

Unfortunately, the microformats plugin calls serendipity_smarty_init() with no assignable variables. (Other plugins may do this, too; I don't mean to single out microformats.) When we return to genpage.inc.php after the genpage hook, we call serendipity_smarty_init() with the set of variables to be assigned (including $view), but serendipity_smarty_init() sees that it's already initialized once, and skips the variable assignments.

By Garvin's great foresight, we pass the list of variables to initialize with the genpage hook. So the right thing to do in a plugin is not to initialize Smarty and assign variables, but rather to add the necessary initializations to the $addData.

In short, I changed the microformats plugin (starting on line 199) from this:

Code: Select all

                case 'genpage':
                    include_once(dirname(__FILE__).'/smarty.inc.php');
                    if (!isset($serendipity['smarty'])) {
                        serendipity_smarty_init();
                    }
                    $serendipity['smarty']->assign('subnode', ($this->get_config('subnode') ? 1 : 0));
                    $serendipity['smarty']->assign('best', $this->get_config('best'));
                    $serendipity['smarty']->assign('step', $this->get_config('step'));
                    $serendipity['smarty']->assign('timezone', $this->get_config('timezone'));
                    $serendipity['smarty']->register_function('microformats_show', 'microformats_serendipity_show');
                    break;

To this:

Code: Select all

                case 'genpage':
                    $addData['subnode'] = ($this->get_config('subnode') ? 1 : 0);
                    $addData['best'] = $this->get_config('best');
                    $addData['step'] = $this->get_config('step');
                    $addData['timezone'] = $this->get_config('timezone');
                    $addData['microformats_show'] = 'microformats_serendipity_show';
                    break;


When the genpage hook is done, all that additional data should be added to the list of things that get assigned when Smarty is initialized. Tested only to verify that nothing blows up; commited; should be available withing 24 hours through SPARTACUS.

Morals of the story:
  1. Plugins shouldn't call serendipity_smarty_init().
  2. Garvin has already thought of everything.

Posted: Tue Jun 17, 2008 10:26 am
by garvinhicking
Hi!

Thanks for that patch! Now I see the problem.

With your patch however you removed the register_function() call to a microformat function. Was this no longer necessary? Does it work without it?

Regards,
Garvin

Posted: Tue Jun 17, 2008 5:14 pm
by judebert
Oh, bloody H. I didn't even recognize that was a different call.

Nothing blows up, and my frontpage event list still looks OK, but I'll bet the microformats don't work very well now.

Hmmm, there's no way to pass functions to register on init. Maybe Garvin didn't think of everything.

Well, the easy thing to do is just add the function during the "frontend_header" hook. By then the "genpage" hook has already been called, and Smarty is already initialized (it has to be, because frontend_header is hooked from a template). Alternatively, I could just check at each hook, and add it for the first hook that doesn't have it.

In the future, it might be nice to have a 'Smarty initialization complete' hook.

Meanwhile, I'm updating the plugin to fix my error. I'm also adding a 'path' variable (copied from the lightbox plugin), so users don't have to edit their index.tpl and insert a '<link>' tag.

Is it plugins that you need to define strings in every language, or the main code? I can never remember.

Posted: Tue Jun 17, 2008 7:48 pm
by garvinhicking
Hi!

One can register smarty functions any time before the smarty output is called...so frontend_header sounds like a suitable register_function call to me?!

Is it plugins that you need to define strings in every language, or the main code? I can never remember.


New language constants are only required in each file of the main s9y release. They wouldn't hurt for plugins though, so you can simply try to remember "put it in everywhere", that has the least screw up factor ;-) ;-)

Regards,
Garvin