Zum Inhalt springen

Eigenes Template für eigen Extension verwenden?

Erstellt am 20. Oktober 2008 · 4 Antworten · letzte Antwort am 27. Oktober 2008

Tags: Templates & Layouts, Frage

florist ·

Hallo!
Ich versuche mit Hilfe des Buchs "Typo3 und TS Kochbuch" vergeblich meiner erstellten Extension ein Template hinzuzufügen.
Ich habe folgenden Code, den ich in die main() - Methode schreiben muss. Doch egal an welcher Stelle ich den Code einfüge, bekomme ich Fehlermeldungen bzw. keine ausgabe.
Listenansicht:

        $this->templateCode = $this->cObj->fileResource($conf['templateFile']);
        $template['total'] = $this->cObj->getSubpart($this->templateCode, '###LISTVIEW###');
        $template['item']  = $this->cObj->getSubpart($template['total'], '###ITEM###');
        while ($row = $GLOABLS['TYPO3_DB']->sql_fetch_assoc($res)) {
            $markerArray['###VALUE1###'] = $row['value1'];
            $markerArray['###VALUE2###'] = $row['value2'];
            $content_item .= $this->cObj->substituteMarkerArrayCached($template['item'], $markerArray);
        }
        $subpartArray['###CONTENT###'] = $content_item;
        $content = $this->cObj->substituteMarkerArrayCached($template['total'], array(), $subpartArray);
        return $content;

Einzelansicht:

        $this->templateCode = $this->cObj->fileResource($conf['templateFile']);
        $template['total'] = $this->cObj->getSubpart($this->templateCode, '###SINGLEVIEW###');
        $markerArray['###FIELD1###'] = $this->pi_getLL('FIELD1');
        $markerArray['###VALUE1###'] = 'VALUE1';
        $markerArray['###FIELD2###'] = $this->pi_getLL('FIELD2');
        $markerArray['###VALUE2###'] = 'VALUE2';
        $content->$this->cObj->substituteMarkerArrayCached($template['total'], $markerArray);
        return $content;

Allerdings versteh ich nicht ganz an welche stelle.
Hier meine main() frisch aus dem Kickstarter:

<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2008 Bernd Müller, Florian Reif <info@approx.de>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

require_once(PATH_tslib.'class.tslib_pibase.php');


/**
 * Plugin 'Veranstaltungs-Plugin' for the 'app_veranstaltung' extension.
 *
 * @author    Bernd Müller, Florian Reif <info@approx.de>
 * @package    TYPO3
 * @subpackage    tx_appveranstaltung
 */
class tx_appveranstaltung_pi1 extends tslib_pibase {
    var $prefixId      = 'tx_appveranstaltung_pi1';        // Same as class name
    var $scriptRelPath = 'pi1/class.tx_appveranstaltung_pi1.php';    // Path to this script relative to the extension dir.
    var $extKey        = 'app_veranstaltung';    // The extension key.
    var $pi_checkCHash = true;
   
    /**
     * Main method of your PlugIn
     *
     * @param    string        $content: The content of the PlugIn
     * @param    array        $conf: The PlugIn Configuration
     * @return    The content that should be displayed on the website
     */
    function main($content,$conf)    {
   
   
   
        switch((string)$conf['CMD'])    {
            case 'singleView':
                list($t) = explode(':',$this->cObj->currentRecord);
                $this->internal['currentTable']=$t;
                $this->internal['currentRow']=$this->cObj->data;
                return $this->pi_wrapInBaseClass($this->singleView($content,$conf));



            break;
            default:
                if (strstr($this->cObj->currentRecord,'tt_content'))    {
                    $conf['pidList'] = $this->cObj->data['pages'];
                    $conf['recursive'] = $this->cObj->data['recursive'];
                }
                return $this->pi_wrapInBaseClass($this->listView($content,$conf));
            break;
        }
    }
   
    /**
     * Shows a list of database entries
     *
     * @param    string        $content: content of the PlugIn
     * @param    array        $conf: PlugIn Configuration
     * @return    HTML list of table entries
     */
    function listView($content,$conf)    {
        $this->conf=$conf;        // Setting the TypoScript passed to this function in $this->conf
        $this->pi_setPiVarDefaults();
        $this->pi_loadLL();        // Loading the LOCAL_LANG values
       
        $lConf = $this->conf['listView.'];    // Local settings for the listView function
   
        if ($this->piVars['showUid'])    {    // If a single element should be displayed:
            $this->internal['currentTable'] = 'tx_appveranstaltung_wittveranstaltungstermin';
            $this->internal['currentRow'] = $this->pi_getRecord('tx_appveranstaltung_wittveranstaltungstermin',$this->piVars['showUid']);
   
            $content = $this->singleView($content,$conf);
            return $content;
        } else {
            $items=array(
                '1'=> $this->pi_getLL('list_mode_1','Mode 1'),
                '2'=> $this->pi_getLL('list_mode_2','Mode 2'),
                '3'=> $this->pi_getLL('list_mode_3','Mode 3'),
            );
            if (!isset($this->piVars['pointer']))    $this->piVars['pointer']=0;
            if (!isset($this->piVars['mode']))    $this->piVars['mode']=1;
   
                // Initializing the query parameters:
            list($this->internal['orderBy'],$this->internal['descFlag']) = explode(':',$this->piVars['sort']);
            $this->internal['results_at_a_time']=t3lib_div::intInRange($lConf['results_at_a_time'],0,1000,3);        // Number of results to show in a listing.
            $this->internal['maxPages']=t3lib_div::intInRange($lConf['maxPages'],0,1000,2);;        // The maximum number of "pages" in the browse-box: "Page 1", "Page 2", etc.
            $this->internal['searchFieldList']='zeitvon,zeitbis,anzahlkarten,kommentar';
            $this->internal['orderByList']='uid,zeitvon,zeitbis,anzahlkarten';
   
                // Get number of records:
            $res = $this->pi_exec_query('tx_appveranstaltung_wittveranstaltungstermin',1);
            list($this->internal['res_count']) = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
   
                // Make listing query, pass query to SQL database:
            $res = $this->pi_exec_query('tx_appveranstaltung_wittveranstaltungstermin');
            $this->internal['currentTable'] = 'tx_appveranstaltung_wittveranstaltungstermin';
   
                // Put the whole list together:
            $fullTable='';    // Clear var;
        #    $fullTable.=t3lib_div::view_array($this->piVars);    // DEBUG: Output the content of $this->piVars for debug purposes. REMEMBER to comment out the IP-lock in the debug() function in t3lib/config_default.php if nothing happens when you un-comment this line!
   
                // Adds the mode selector.
            $fullTable.=$this->pi_list_modeSelector($items);
   
                // Adds the whole list table
            $fullTable.=$this->pi_list_makelist($res);
   
                // Adds the search box:
            $fullTable.=$this->pi_list_searchBox();
   
                // Adds the result browser:
            $fullTable.=$this->pi_list_browseresults();
   
                // Returns the content from the plugin.
            return $fullTable;
        }
    }
    /**
     * Display a single item from the database
     *
     * @param    string        $content: The PlugIn content
     * @param    array        $conf: The PlugIn configuration
     * @return    HTML of a single database entry
     */
    function singleView($content,$conf)    {
        $this->conf=$conf;
        $this->pi_setPiVarDefaults();
        $this->pi_loadLL();
       
       
        $this->templateCode = $this->cObj->fileResource($conf['templateFile']);
        $template['total'] = $this->cObj->getSubpart($this->templateCode, '###SINGLEVIEW###');
   
            // This sets the title of the page for use in indexed search results:
        if ($this->internal['currentRow']['title'])    $GLOBALS['TSFE']->indexedDocTitle=$this->internal['currentRow']['title'];
   
        $content='<div'.$this->pi_classParam('singleView').'>
            <h2>Record "'.$this->internal['currentRow']['uid'].'" from table "'.$this->internal['currentTable'].'":</h2>
            <table>
                <tr>
                    <td nowrap="nowrap" valign="top" this-="">pi_classParam('singleView-HCell').'><p>'.$this->getFieldHeader('vid').'</p></td>
                    <td valign="top"><p>'.$this->getFieldContent('vid').'</p></td>
                </tr>
                <tr>
                    <td nowrap="nowrap" valign="top" this-="">pi_classParam('singleView-HCell').'><p>'.$this->getFieldHeader('tagvon').'</p></td>
                    <td valign="top"><p>'.$this->getFieldContent('tagvon').'</p></td>
                </tr>
                <tr>
                    <td nowrap="nowrap" valign="top" this-="">pi_classParam('singleView-HCell').'><p>'.$this->getFieldHeader('tagbis').'</p></td>
                    <td valign="top"><p>'.$this->getFieldContent('tagbis').'</p></td>
                </tr>
                <tr>
                    <td nowrap="nowrap" valign="top" this-="">pi_classParam('singleView-HCell').'><p>'.$this->getFieldHeader('zeitvon').'</p></td>
                    <td valign="top"><p>'.$this->getFieldContent('zeitvon').'</p></td>
                </tr>
                <tr>
                    <td nowrap="nowrap" valign="top" this-="">pi_classParam('singleView-HCell').'><p>'.$this->getFieldHeader('zeitbis').'</p></td>
                    <td valign="top"><p>'.$this->getFieldContent('zeitbis').'</p></td>
                </tr>
                <tr>
                    <td nowrap="nowrap" valign="top" this-="">pi_classParam('singleView-HCell').'><p>'.$this->getFieldHeader('anzahlkarten').'</p></td>
                    <td valign="top"><p>'.$this->getFieldContent('anzahlkarten').'</p></td>
                </tr>
                <tr>
                    <td nowrap="nowrap" valign="top" this-="">pi_classParam('singleView-HCell').'><p>'.$this->getFieldHeader('freigabe').'</p></td>
                    <td valign="top"><p>'.$this->getFieldContent('freigabe').'</p></td>
                </tr>
                <tr>
                    <td nowrap="nowrap" valign="top" this-="">pi_classParam('singleView-HCell').'><p>'.$this->getFieldHeader('kommentar').'</p></td>
                    <td valign="top"><p>'.$this->getFieldContent('kommentar').'</p></td>
                </tr>
                <tr>
                    <td nowrapthis-="">pi_classParam('singleView-HCell').'><p>Last updated:</p></td>
                    <td valign="top"><p>'.date('d-m-Y H:i',$this->internal['currentRow']['tstamp']).'</p></td>
                </tr>
                <tr>
                    <td nowrapthis-="">pi_classParam('singleView-HCell').'><p>Created:</p></td>
                    <td valign="top"><p>'.date('d-m-Y H:i',$this->internal['currentRow']['crdate']).'</p></td>
                </tr>
            </table>
        <p>'.$this->pi_list_linkSingle($this->pi_getLL('back','Back'),0).'</p></div>'.
        $this->pi_getEditPanel();
   
        return $content;
    }
    /**
     * Returns a single table row for list view
     *
     * @param    integer        $c: Counter for odd / even behavior
     * @return    A HTML table row
     */
    function pi_list_row($c)    {
        $editPanel = $this->pi_getEditPanel();
        if ($editPanel)    $editPanel='<td>'.$editPanel.'</td>';
   
        return '<tr'.($c%2 this-="">pi_classParam('listrow-odd') : '').'>
                <td><p>'.$this->getFieldContent('uid').'</p></td>
                <td valign="top"><p>'.$this->getFieldContent('vid').'</p></td>
                <td valign="top"><p>'.$this->getFieldContent('tagvon').'</p></td>
                <td valign="top"><p>'.$this->getFieldContent('tagbis').'</p></td>
                <td valign="top"><p>'.$this->getFieldContent('zeitvon').'</p></td>
                <td valign="top"><p>'.$this->getFieldContent('zeitbis').'</p></td>
                <td valign="top"><p>'.$this->getFieldContent('anzahlkarten').'</p></td>
                <td valign="top"><p>'.$this->getFieldContent('freigabe').'</p></td>
            </tr>';
    }
    /**
     * Returns a table row with column names of the table
     *
     * @return    A HTML table row
     */
    function pi_list_header()    {
        return '<tr'.$this->pi_classParam('listrow-header').'>
                <td><p>'.$this->getFieldHeader_sortLink('uid').'</p></td>
                <td nowrap=""><p>'.$this->getFieldHeader('vid').'</p></td>
                <td nowrap=""><p>'.$this->getFieldHeader('tagvon').'</p></td>
                <td nowrap=""><p>'.$this->getFieldHeader('tagbis').'</p></td>
                <td><p>'.$this->getFieldHeader_sortLink('zeitvon').'</p></td>
                <td><p>'.$this->getFieldHeader_sortLink('zeitbis').'</p></td>
                <td><p>'.$this->getFieldHeader_sortLink('anzahlkarten').'</p></td>
                <td nowrap=""><p>'.$this->getFieldHeader('freigabe').'</p></td>
            </tr>';
    }
    /**
     * Returns the content of a given field
     *
     * @param    string        $fN: name of table field
     * @return    Value of the field
     */
    function getFieldContent($fN)    {
        switch($fN) {
            case 'uid':
                return $this->pi_list_linkSingle($this->internal['currentRow'][$fN],$this->internal['currentRow']['uid'],1);    // The "1" means that the display of single items is CACHED! Set to zero to disable caching.
            break;
            case "tagvon":
                    // For a numbers-only date, use something like: %d-%m-%y
                return strftime('%A %e. %B %Y',$this->internal['currentRow']['tagvon']);
            break;
            case "tagbis":
                    // For a numbers-only date, use something like: %d-%m-%y
                return strftime('%A %e. %B %Y',$this->internal['currentRow']['tagbis']);
            break;
            default:
                return $this->internal['currentRow'][$fN];
            break;
        }
    }
    /**
     * Returns the label for a fieldname from local language array
     *
     * @param    [type]        $fN: ...
     * @return    [type]        ...
     */
    function getFieldHeader($fN)    {
        switch($fN) {
           
            default:
                return $this->pi_getLL('listFieldHeader_'.$fN,'['.$fN.']');
            break;
        }
    }
   
    /**
     * Returns a sorting link for a column header
     *
     * @param    string        $fN: Fieldname
     * @return    The fieldlabel wrapped in link that contains sorting vars
     */
    function getFieldHeader_sortLink($fN)    {
        return $this->pi_linkTP_keepPIvars($this->getFieldHeader($fN),array('sort'=>$fN.':'.($this->internal['descFlag']?0:1)));
    }
}



if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/app_veranstaltung/pi1/class.tx_appveranstaltung_pi1.php'])    {
    include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/app_veranstaltung/pi1/class.tx_appveranstaltung_pi1.php']);
}

?>

Und so sieht meine veranstaltungs-templ.html aus:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML-Template</title>
</head>
<body>
    <h3>HTML-Template</h3>
    <!-- ###LISTVIEW### begin -->
    <table>
        <!-- ###CONTENT### -->
        <!-- ###ITEM### -->
        <tr>
            <td>###VALUE1###</td><td>###VALUE2###</td>
        </tr>
        <!-- ###CONTENT### -->
        <!-- ###ITEM### -->
    </table>
    <!-- ###SINGLEVIEW### begin -->
    <dl>
        <dt>###FIELD1###</dt>
            <dd>###VALUE1###</dd>
        <dt>###FIELD2</dt>
            <dd>###VALUE2</dd>
    </dl>
    <!-- ###SINGLEVIEW### end-->
</body>
</html>

Wäre super wenn sich das jemand kurz anschauen kann und mir aml auf die Sprünge hilft, bin am Ende mit meinem Latein!
Gruß Flo

ÁndreMansfeld ·

Hallo florist,
ich habs nur kurz überflogen, aber wenn ich nichts übersehen habe und du alles gepostet hast, dann glaube ich, dass du nirgendwo die Quelle für das Template definierst.
Mit

$this->templateCode = $this->cObj->fileResource($conf['templateFile']);

gibst du zwar die Parameter mit aber $conf['templateFile'] mußt du noch definieren.
Entweder in der SETUP Datei mit

templateFile = fileadmin/...../Template.htm oder tmpl

oder direkt in der Extension _pi.

Andreas

florist ·

Hi!
Ups, hab ich wohl vergessen zu posten.
Ich hab in meinem Roottemplate den Pfad zur Templatedatei angegeben:

plugin.[nameDerExtension]_pi1.templateFile = fileadmin/templatedaten/forms/[nameDerExtension]-tmpl.html

Mir wäre wichtig zu wissen oder zu verstehen, an welcher Stelle der main() ich den geposteten PHP-Code einfügen muss.

ÁndreMansfeld ·

Sowohl in Zeile 79 als auch in 143 werden die Funktionen ausgeführt.
Dort sollte auch stehen welches Template verwendet werden soll.
Für die Singelansicht stehts in Zeile 149 und 150.
Dort sollte es auch funktionieren.
Für die Listenansicht muß es dann noch rein (wie bei Singleansicht)
Die Marker gehören dann in das Ausgabe Query.
Hast du schonmal versucht direkt das Template anzusprechen, ohne die Ausgaben. Nur um zu sehen ob das Richtige angezeigt wird.

function listView($content,$conf)    {
$this->templateCode = $this->cObj->fileResource($conf['templateFile']);
}

stark vereinfacht 😃
Kannst du dann auf die Marker/Row u.s.w. erweitern.

Andreas

florist ·

Ah ok, langsam lichtet sich das dunkel.
Danke dir erstmal, bist mir eine sehr große Hilfe!
Allerdings noch nicht ganz. Da ich eigentlich mit PHP und MySQL nicht sovie lzutun habe ergeben sich noch Fragen.

1. was ist die "Ausgabe Query"?

2."Hast du schonmal versucht direkt das Template anzusprechen, ohne die Ausgaben" -wie mach ich das?

3.Was meinst du mit auf "Marker/Rows" erweitern

Wär echt super, wenn du mir noch weiterhelfen könntest, wie gesagt is meine 1. Extension und PHP lernt man leider nichmal schnell in 2-3 Monaten :o

Also ich bekomme keine Fehlermeldungen mehr, allerdings wird auch nichts angezeigt.
hab jetzt folgenden Code für die listenansicht:

function listView($content,$conf)	{
		$this->conf=$conf;		// Setting the TypoScript passed to this function in $this->conf
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();		// Loading the LOCAL_LANG values
      		$template['total'] = $this->cObj->getSubpart($this->templateCode, '###LISTVIEW###');
       		$template['item']  = $this->cObj->getSubpart($template['total'], '###ITEM###');
       		
		
		$lConf = $this->conf['listView.'];	// Local settings for the listView function
	
		if ($this->piVars['showUid'])	{	// If a single element should be displayed:
			$this->internal['currentTable'] = 'tx_appveranstaltung_wittveranstaltungstermin';
			$this->internal['currentRow'] = $this->pi_getRecord('tx_appveranstaltung_wittveranstaltungstermin',$this->piVars['showUid']);
	
			$content = $this->singleView($content,$conf);
			return $content;
		} else {
			$items=array(
				'1'=> $this->pi_getLL('list_mode_1','Mode 1'),
				'2'=> $this->pi_getLL('list_mode_2','Mode 2'),
				'3'=> $this->pi_getLL('list_mode_3','Mode 3'),
			);
			if (!isset($this->piVars['pointer']))	$this->piVars['pointer']=0;
			if (!isset($this->piVars['mode']))	$this->piVars['mode']=1;
	
				// Initializing the query parameters:
			list($this->internal['orderBy'],$this->internal['descFlag']) = explode(':',$this->piVars['sort']);
			$this->internal['results_at_a_time']=t3lib_div::intInRange($lConf['results_at_a_time'],0,1000,3);		// Number of results to show in a listing.
			$this->internal['maxPages']=t3lib_div::intInRange($lConf['maxPages'],0,1000,2);;		// The maximum number of "pages" in the browse-box: "Page 1", "Page 2", etc.
			$this->internal['searchFieldList']='zeitvon,zeitbis,anzahlkarten,kommentar';
			$this->internal['orderByList']='uid,zeitvon,zeitbis,anzahlkarten';
			

	
				// Get number of records:
			$res = $this->pi_exec_query('tx_appveranstaltung_wittveranstaltungstermin',1);
			list($this->internal['res_count']) = $GLOBALS['TYPO3_DB']->sql_fetch_row($res);
	
				// Make listing query, pass query to SQL database:
			$res = $this->pi_exec_query('tx_appveranstaltung_wittveranstaltungstermin');
			$this->internal['currentTable'] = 'tx_appveranstaltung_wittveranstaltungstermin';
			
			
			
						
			while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
				$markerArray['###VALUE1###'] = $row['name'];
				$markerArray['###VALUE2###'] = $row['ort'];
				$content_item .= $this->cObj->substituteMarkerArrayCached($template['item'], $markerArray);
			}
			$subpartArray['###CONTENT###'] = $content_item;
			$content = $this->cObj->substituteMarkerArrayCached($template['total'], array(), $subpartArray);
			return $content;



			
	
				// Put the whole list together:
			$fullTable='';	// Clear var;
		#	$fullTable.=t3lib_div::view_array($this->piVars);	// DEBUG: Output the content of $this->piVars for debug purposes. REMEMBER to comment out the IP-lock in the debug() function in t3lib/config_default.php if nothing happens when you un-comment this line!
	
				// Adds the mode selector.
			$fullTable.=$this->pi_list_modeSelector($items);
	
				// Adds the whole list table
			$fullTable.=$this->pi_list_makelist($res);
	
				// Adds the search box:
			$fullTable.=$this->pi_list_searchBox();
	
				// Adds the result browser:
			$fullTable.=$this->pi_list_browseresults();
	
				// Returns the content from the plugin.
			return $fullTable;
			
			while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
				$markerArray['###VALUE1###'] = $row['name'];
				$markerArray['###VALUE2###'] = $row['ort'];
				$content_item .= $this->cObj->substituteMarkerArrayCached($template['item'], $markerArray);
			}
			$subpartArray['###CONTENT###'] = $content_item;
			$content = $this->cObj->substituteMarkerArrayCached($template['total'], array(), $subpartArray);
			return $content;

		}
	}

Und hier die Einzelansicht:

function singleView($content,$conf)	{
		$this->conf=$conf;
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();
 		$this->templateCode = $this->cObj->fileResource($conf['templateFile']);
		$template['total'] = $this->cObj->getSubpart($this->templateCode, '###SINGLEVIEW###');
		
	
			// This sets the title of the page for use in indexed search results:
		if ($this->internal['currentRow']['title'])	$GLOBALS['TSFE']->indexedDocTitle=$this->internal['currentRow']['title'];
	
		$this->templateCode = $this->cObj->fileResource($conf['templateFile']);
		$template['total'] = $this->cObj->getSubpart($this->templateCode, '###SINGLEVIEW###');
		$markerArray['###FIELD1###'] = $this->pi_getLL('FIELD1');
		$markerArray['###VALUE1###'] = 'name';
		$markerArray['###FIELD2###'] = $this->pi_getLL('FIELD2');
		$markerArray['###VALUE2###'] = 'ort';
		$content->$this->cObj->substituteMarkerArrayCached($template['total'], $markerArray);
		return $content;

		
		$this->pi_getEditPanel();
	
	}