Bug tracker

This ticket has been moved to our new tracker. Open Ticket PHPBB3-8778 now.

Issues with template->destroy and template->set_custom_template (fix completed in vcs)

I have a mod that instantiates a new template object then recursively uses the object to display different blocks, setting a new custom template path (template->set_custom_template) for each block, setting a template file (template->set_filenames), destroys the template, then starts over again. It would look something like this:

Code: Select all
$mtemplate = new template();
foreach ($blocks as $row)
{
      $mtemplate->set_custom_template(...);
      $data = call_block_function($row);
      $template->assign_block_vars('sidebar', array(
            'BLOCK_TITLE'        => $data['title'],
            'BLOCK_CONTENT'  => $data['content'])
      );
      $mtemplate->destroy();
}


This worked fine prior to 3.0.6 but as of 3.0.6-RC1, destroying the template leads to template issues: Some blocks turn up blank, some variables are not assigned and things like that. I have to destroy the template because some blocks are called more than ones and use the same template file to display different things based on the block settings. I have not been able to figure out the problem but it almost seems when the template is destroyed, template->set_filenames must be set first (which is inconvenient in some instances) prior to assigning any template variables. I'm I missing something or do I smell a bug? :)

Comments / History

Posted by Acyd Burn (Server Manager) on Oct 7th 2009, 12:18

Do you have a working example with which we are able to reproduce this? At the moment i can see no code change which would cause this.

Posted by Blitze on Oct 8th 2009, 07:25

Sorry should have provided some links. Here is an installation of 3.0.6-RC2 with my mod. You can try login in using username "xxx" and password "xxx". Notice that if you try to login using the login block, nothing happens, mainly because template variables are not set when _tpldata is empty. Also notice the blank box on the left. This does not seem to happen with assign_block_vars.

You can see the source for the function that processes the blocks and the Login block, and here is a 3.0.5 installation.
Hope this clarifies the issue. I have tried everything I could but just couldn't figure what's wrong.

edit by acydburn: [removed the admin credentials]

Posted by Acyd Burn (Server Manager) on Oct 8th 2009, 13:29

Can you provide simple code to reproduce this? Just post it in [code] blocks - a forum installation will not help here and i do not want to install a MOD if i can avoid this. ;)

Posted by Acyd Burn (Server Manager) on Oct 8th 2009, 13:39

An example (which works fine for me)

test.php:
Code: Select all
<?php
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();

$mtemplate = new template();
$mtemplate->set_custom_template('styles/prosilver/template/', 'prs');
$mtemplate->set_filenames(array('body' => 'test.html'));

$mtemplate->assign_block_vars('something', array(
   'LOOP'      => 1,
   'LOOP2'      => 2,
));

$mtemplate->display('body');

print_r($mtemplate);

$mtemplate->destroy();

$mtemplate->assign_block_vars('something', array(
   'LOOP'      => 3,
   'LOOP2'      => 4,
));

$mtemplate->display('body');
print_r($mtemplate);

exit;


test.html
Code: Select all
BEGIN
<!-- BEGIN something -->
{something.LOOP}{something.LOOP2}
<!-- END something -->
END

Edited post #190665

Action performed by Acyd Burn (Server Manager) on Oct 8th 2009, 13:40

Posted by blueray2048 on Oct 8th 2009, 15:18

@Blitze

Sorry for interfere the post.
I saw you have used different template objects.
May I ask why you do so ?

Thanks,

Posted by Blitze on Oct 8th 2009, 17:25

Say I have a template file located here root/styles/prosilver/template/portal.html looking like this:
Code: Select all
<div id="sidebar" class="left-box" style="width: 20%">
   <!-- BEGIN sidebar -->
      <div class="block'>
         <h3>{sidebar.BLOCK_TITLE}</h3>
         <span>{sidebar.BLOCK_CONTENT}</span>
      </div>
   <!-- END sidebar -->
</div>
<div id="main-content" class="left-box" style="width: 60%">
   // stuff
</div>

The typical structure for the mod looks like this:
Code: Select all
- root/
   - modules/
      - example_module/
         - blocks/
            - sample_block.php
         - template/
            - sample_block_template.html

Say the sample_block.php looks like this:
Code: Select all
<?php
function portal_sample_block($row)
{
   global $mtemplate, $phpbb_root_path;

   $mtemplate->assign_vars(array(
      'TEST1'   => '1st test',
      'TEST2'   => '2nd test')
   );

   $tpl_path = $phpbb_root_path . 'modules/example_module/template/'
   $mtemplate->set_custom_template($tpl_path, 'test');
   $mtemplate->set_filenames(array('body' => 'sample_block_template.html'));

   return array(
      'title'      => 'My sample block',
      'content'      => $mtemplate->assign_display('block')
   );
}
?>

and sample_block_template.html
Code: Select all
<div class="bg3">{TEST1}</div>
<div class="bg2">{TEST2}</div>

Then say I have this file that processes the blocks
Code: Select all
$mtemplate = new template();
foreach ($block as $block_row)
{
   $module = $row['module'];
   $block_name = $row['block_name'];
   $func = 'portal_' . $block_name;

   if (!function_exists($func))
   {
      @include($phpbb_root_path . "modules/$module/blocks/$block_name.$phpEx");
   }

   //call block function
   $data = $func($row);

   $template->assign_block_vars('sidebar', array(
      'BLOCK_TITLE'      => $data['title'],
      'BLOCK_CONTENT'   => $data['content'])
   );
   $mtemplate->destroy();
}
This worked fine prior to 3.0.6. Blocks that use template->assign_block_vars seem to work fine but template->assign_vars is where I'm having problems after template->destroy is used.

I'd hate to take up your time if this is something that I caused myself but this worked before and I'm not sure what went wrong.

@blueray2048
I use a separate template object because I use template->assign_display(), and template->destroy on blocks which would clear out the template data. Hope I'm making sense.

Posted by ToonArmy (Development Team Member) on Oct 8th 2009, 19:49

I'm guessing the problem is this change: http://code.phpbb.com/repositories/diff ... p?rev=9811

Edited post #190815

Action performed by Blitze on Oct 9th 2009, 03:45

Posted by Blitze on Oct 9th 2009, 03:48

Ah I think I got a fix. Changing this:
Code: Select all
   function destroy()
   {
      $this->_tpldata = array('.' => array(0 => array()));
   }

to this:
Code: Select all
   function destroy()
   {
      $this->_rootref = array('.' => array(0 => array()));
   }

seems to do the trick.

Ticket details

Related SVN changesets