Seite 1 von 1

Knowledge Base Funktion für Template Parsing ändern

Verfasst: 02.02.2012 16:58
von WileCoyote
Ich würde mal die Hilfe eines erfahrenen Coders benötigen.
Da es im Moment keine brauchbare Alternative gibt teste ich gerade die [ABD] PhpBB3 Knowledge Base MOD.
In der Datei includes/functions_kb_plugins.php gibt es folgende Funktion zum Parsen der Plugin Templates

Code: Alles auswählen

/**
* Parse a template and return the parsed text
*
* This function should be used for ALL plugins that normally use $template->assign_display to output data with the plugin system.
*/
function kb_parse_template($filename, $template_file)
{
	global $phpbb_root_path, $template, $user;

	$tpl_path = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template/kb/plugins/';
	
	// If the template file does not exist
	if (!file_exists($tpl_path . $template_file))
	{
		$error = sprintf($user->lang['PLUGIN_TEMPLATE_MISSING'], $template_file);
	
		trigger_error($error);
	}
	
	$template_path = 'kb/plugins/';

	$template->set_filenames(array(
		$filename		=> $template_path . $template_file,
	));

	// return the output
	return $template->assign_display($filename);
}
Das Problem ist, dass damit keine "Template inheritance" funktioniert. Daher habe ich die Zeile:

Code: Alles auswählen

	$tpl_path = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template/kb/plugins/';
dadurch ersetzt

Code: Alles auswählen

    if (file_exists($phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template/kb/plugins/'))
	{
        $tpl_path = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template/kb/plugins/';	   
	}
	else if (file_exists($phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template/kb/plugins/'))
    {
        $tpl_path = $phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template/kb/plugins/';	           
    }
Das funktioniert zwar, ich habe aber das Gefühl, dass man das besser lösen könnte. Weiß jemand Rat?

Re: Knowledge Base Funktion für Template Parsing ändern

Verfasst: 02.02.2012 18:49
von Miriam
So vllt? ->

Code: Alles auswählen

function kb_parse_template($filename, $template_file)
{
    global $phpbb_root_path, $template, $user;

    if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'])
    {
        $tpl_path = $phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template/kb/plugins/';
        
        // If the template file does not exist
        if (!@file_exists($tpl_path . $template_file))
        {
            $error = sprintf($user->lang['PLUGIN_TEMPLATE_MISSING'], $template_file);
            trigger_error($error);
        }
    }
    $tpl_path = 'kb/plugins/';

    $template->set_filenames(array(
        $filename      => $tpl_path . $template_file,
    ));

    // return the output
    return $template->assign_display($filename);
} 
(Hab's nicht getestet.)

Re: Knowledge Base Funktion für Template Parsing ändern

Verfasst: 02.02.2012 19:07
von WileCoyote
Eben getestet und funktioniert. Danke dir. Ich wußte doch, dass man das besser lösen kann. Muß da wohl noch viel lernen.

Re: Knowledge Base Funktion für Template Parsing ändern

Verfasst: 02.02.2012 19:23
von Miriam
Ich auch... :grin: Probiere mal den hier:

Code: Alles auswählen

function kb_parse_template($filename, $template_file)
{
    global $phpbb_root_path, $template, $user;

    if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherits_id'])
    {
        $tpl_path = $phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template/kb/plugins/';
        
        // If the template file does not exist
        if (!@file_exists($tpl_path . $template_file))
        {
            $error = sprintf($user->lang['PLUGIN_TEMPLATE_MISSING'], $template_file);
            trigger_error($error);
        }
    }
    else
    {
        $tpl_path = 'kb/plugins/';
    }
    
    $template->set_filenames(array(
        $filename      => $tpl_path . $template_file,
    ));

    // return the output
    return $template->assign_display($filename);
}  
Bei dem anderen hat was gefehlt. *hüstel*

Re: Knowledge Base Funktion für Template Parsing ändern

Verfasst: 02.02.2012 19:51
von WileCoyote
Also im Gegensatz zur vorherigen Variante funktioniert die nicht

Code: Alles auswählen

Allgemeiner Fehler
template->_tpl_load_file(): File ./styles/prosilver/template/./styles/prosilver/template/kb/plugins/request_list.html does not exist or is empty
Edit: Ich glaube, folgender Code sollte richtig sein

Code: Alles auswählen

function kb_parse_template($filename, $template_file)
{
    global $phpbb_root_path, $template, $user;
    
    //Inherited
    if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherit_path'])
    {
        $tpl_path = $phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template/kb/plugins/';        
    }
    //not inherited
    else
    {
        $tpl_path = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template/kb/plugins/';
    }
    
    // If the template file does not exist
    if (!@file_exists($tpl_path . $template_file))
    {
        $error = sprintf($user->lang['PLUGIN_TEMPLATE_MISSING'], $template_file);
    
        trigger_error($error);
    }        
    
    $template_path = 'kb/plugins/';
    
    $template->set_filenames(array(
        $filename        => $template_path . $template_file,
    ));
    
    // return the output
    return $template->assign_display($filename);
}
 

Re: Knowledge Base Funktion für Template Parsing ändern

Verfasst: 03.02.2012 12:18
von Miriam
Ich neme alles zurück und behaupte das Gegenteil :D
Mein erster Code war richtig. Deiner ist es auch.

In deinem Code ist das hier nicht nötig ->

Code: Alles auswählen

    //not inherited
    else
    {
        $tpl_path = $phpbb_root_path . 'styles/' . $user->theme['template_path'] . '/template/kb/plugins/';
    } 
Sollte das Template nicht vererbt worden sein, bekommst du ohnehin eine Fehlermeldung. Falls die kommt, hast du generell ein Problem mit deinem Template. Von daher ist die Abfrage eigentlich sinnlos.
Somit kannst du die Abfrage komplett auf die "Vererbung" konzentrieren, und die Abfrage, ob die "vererbte" Template Datei da ist, in diese IF Abfrage bauen... Spart eine IF-Abfrage.
Und dann sieht dein Code so aus, wie der, den ich als erstes gepostet hatte. ->

Code: Alles auswählen

    //Inherited
    if (isset($user->theme['template_inherits_id']) && $user->theme['template_inherit_path'])
    {
        $tpl_path = $phpbb_root_path . 'styles/' . $user->theme['template_inherit_path'] . '/template/kb/plugins/';        
     
        // If the template file does not exist
        if (!@file_exists($tpl_path . $template_file))
        {
            $error = sprintf($user->lang['PLUGIN_TEMPLATE_MISSING'], $template_file);
    
            trigger_error($error);
        } 
    } 

Re: Knowledge Base Funktion für Template Parsing ändern

Verfasst: 03.02.2012 17:53
von WileCoyote
Hallo Miriam,
prinzipiell hast du recht. Ich habe mich letztendlich für diese Variante entschieden, weil die Abfrage für nicht vererbte Templates in der original Fassung der Datei vorhanden ist. Wahrscheinlich wollten die Autoren dadurch sicher stellen, dass das Plugin-Verzeichnis tatsächlich existiert. Sie hatten einfach die Vererbung nicht bedacht. Bevor ich mich zu der von mir geposteten Veriante entschloss, war ich zum selben Ergebnis gekommen, wie du mit deinem letzten Beitrag.