Zum Inhalt springen

eigene Extension - Problem bei Ausgabe/Ersetzen in Template

Erstellt am 29. November 2004 · 2 Antworten · letzte Antwort am 9. Dezember 2004

Tags: Frage

apu ·

Hallo,

beschäftige mich im rahmen meiner projektarbeit nun seit ca einem Monat mit Typo3, und seit ca 2-3 Wochen mit dem schreiben eigener extenions.
hab das grundgerüst mit dem kickstarter erstellt, und versuche im moment, ein template einzubinden, sodass die ausgaben einer DB-abfrage an die entsprechenden stellen gesetzt werden.

Die Extension soll eine Auflistung von Diplomarbeiten mit Autor, Titel, Betreuer, etc. ausgeben.

Leider steh ich da ziemlich auf dem Schlauch, hab schon google und die Suche in diesem Foruim bemüht, aber letztendlich doch nichts rausbekommen.

Mein template (provisorisch)

<BODY>
<H2>
Header of the page
</H2>
<TABLE WIDTH="600" BORDER="1" CELLSPACING="0" CELLPADDING="0">
  <TR>
    <TH>Art</TH>
    <TH>Arbeit</TH>
    <TH>Betreuer</TH>
    <TH>Semester</TH>
    <TH>Jahr</TH>
  </TR>
  <TR>
   <TD ID="TYPE_OF_THESIS">Diplom</TD>
   <TD ID="AUTHOR">Autor</TD>
   <TD ID="TITLE">Titel</TD>
   <TD ID="PROFESSOR">Professor</TD>
   <TD ID="SEMESTER">Semester</TD>
  </TR>
</TABLE>
</BODY>
</HTML>

TypoScript Setup code für die Seite, auf der die Ausgabe Extension dargestellt werden soll:

# Configuring the Auto-Parser for main template:
plugin.tx_automaketemplate_pi1 {
# Read the template file:
content = FILE
content.file = typo3conf/ext/theses_portal/pi1/template.html
# Here we define which elements in the HTML that
# should be wrapped in subpart-comments:
elements {
BODY.all = 1
BODY.all.subpartMarker = DOCUMENT_BODY
HEAD.all = 1
HEAD.all.subpartMarker = DOCUMENT_ACHEADER
HEAD.rmTagSections = title
TABLE.all = 1
TABLE.all.subpartMarker = TABLE
TR.all = 1
TR.all.subpartMarker = TABLE_ROW
TD.all = 1
}
# Prefix all relative paths with this value:
relPathPrefix = typo3conf/ext/theses_portal/pi1
}
# Main TEMPLATE cObject for the BODY
temp.mainTemplate = TEMPLATE
temp.mainTemplate {
# Feeding the content from the Auto-parser to the TEMPLATE cObject:
template =< plugin.tx_automaketemplate_pi1
# Select only the content between the <body>-tags
workOnSubpart = DOCUMENT_BODY
# Substitute the ###titel### subpart with some example content:
subparts.TITLE= TEXT
subparts.TITLE.value = HELLO WORLD - MENU
}

# Default PAGE object:
page = PAGE
page.typeNum = 0
# Copying the content from TEMPLATE for <body>-section:
page.10 < temp.mainTemplate
# Copying the content from TEMPLATE for <head>-section:
#page.headerData.10 < temp.headTemplate

und nun der "versuch" meiner extension

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

class tx_thesesportal_pi1 extends tslib_pibase {
	var $prefixId = "tx_thesesportal_pi1";		// Same as class name
	var $scriptRelPath = "pi1/class.tx_thesesportal_pi1.php";	// Path to this script relative to the extension dir.
	var $extKey = "theses_portal";	// The extension key.
	
	/**
	 * [Put your description here]
	 */
	function main($content,$conf)	
        {
		$this->conf=$conf;
		$this->pi_setPiVarDefaults();
		$this->pi_loadLL();
                
                // Art der Arbeit, per GET übergeben
                $type_of_thesis=t3lib_div::_GET('type_of_thesis');
                
                $this->templateCode = $this->cObj->fileResource($this->conf["templateFile"]);
                        
                //SELECT-Abfrage, listet alle eingetragenen Arbeiten auf
                $res=$GLOBALS['TYPO3_DB']->
                exec_SELECTquery('tx_thesesportal_thesis.uid, tx_thesesportal_thesis.title, 
                                tx_thesesportal_student.first_name sfirst, tx_thesesportal_student.last_name slast, 
                                tx_thesesportal_professor.first_name pfirst, tx_thesesportal_professor.last_name plast,
                                tx_thesesportal_professor.title ptitle, tx_thesesportal_thesis.semester, tx_thesesportal_thesis.end_date,
                                tx_thesesportal_thesis.extern_company',
                                // END SELECT
                                'tx_thesesportal_thesis, tx_thesesportal_student, tx_thesesportal_professor',  
                                // END FROM
                                'tx_thesesportal_thesis.fk_author=tx_thesesportal_student.uid
                                AND tx_thesesportal_thesis.fk_professor=tx_thesesportal_professor.uid
                                
                                AND tx_thesesportal_thesis.type_of_thesis='.$type_of_thesis); /*.'
                                AND tx_thesesportal_thesis.extern_company IS NOT NULL';*/
                                // END WHERE                                       
                    
                    $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
                    $markerArray['###TITLE###'] = $row["title"];
                    $markerArray['###AUTHOR###'] = $row["sfirst"].' '.$row["slast"];
                       
                    $tablerow = $this->cObj->getSubpart($this->templateCode, '###TABLE_ROW###');
                    $content = $this->cObj->substituteMarkerArrayCached($tablerow, array(), $markerArray, array());   
                    return $this->pi_wrapInBaseClass($content); 
                 
                }                    
}



if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/theses_portal/pi1/class.tx_thesesportal_pi1.php"])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/theses_portal/pi1/class.tx_thesesportal_pi1.php"]);
}

?>

Die Abfrage ist korrekt, die funktioniert so. Schleife fehlt, es ging mir erstmal um eine simple Ersetzung.
Allerdings komm ich mit dem ersetzen im Template nicht klar. Die Dokus erklären solche Dinge leider nicht so ausführlich, wie ich sie in meinem derzeitigen wissensstand benötige 🙁

Falls irgendetwas überflüssig ist (zB im Typo Script Setup Code) lasst es mich bitte auch wissen. Danke schonmal für jegliche Mühen!!

minicoder ·

stehe gerde am selben punkt an.

das ganze in verbindung mit typoscript zu
lösen habe ich noch gar nicht überlegt...
meine extension soll einfach die ensprechenden
markers in meinem template ersetzen, damit
ich die ausgabe per css formatieren kann.

hat niemand eine lösung?
o.O

jwitt ·

Setze Marker in dein Template, etwa so:

<!-- ###thesis### begin -->
<TABLE WIDTH="600" BORDER="1" CELLSPACING="0" CELLPADDING="0">
  <TR>
    <TH>Art</TH>
    <TH>Arbeit</TH>
    <TH>Betreuer</TH>
    <TH>Semester</TH>
    <TH>Jahr</TH>
  </TR>
<!-- ###items### begin -->
<!-- ###item### begin -->
  <TR>
   <TD ID="TYPE_OF_THESIS">###Diplom###</TD>
   <TD ID="AUTHOR">###Autor###</TD>
   <TD ID="TITLE">###Titel###</TD>
   <TD ID="PROFESSOR">###Professor###</TD>
   <TD ID="SEMESTER">###Semester###</TD>
  </TR>
<!-- ###item### end -->
<!-- ###items### end -->
</TABLE>

<!-- ###CUT_irgendwas### begin  -->
            <p>###irgendetwas###</p>
<!-- ###CUT_irgendwas### end  -->


<!-- ###thesis### end -->

Hol dir die Daten aus deiner Tabelle

    $query  = "SELECT * FROM ... WHERE.....";
    $res = mysql(TYPO3_db,$query);
    $dataset = array();
	    while ($row = mysql_fetch_array($res,MYSQL_ASSOC)){
                 $dataset[] = $row;
        }

Dann so weiterverarbeiten:

   $topnode[TYPE] = "thesis"; 
   foreach($dataset as $row){
            unset($node);
	    $node[TYPE] = "item";
	    $node[MARKERS]["Autor"] =$row["sfirst"].' '.$row["slast"]; 
	    $node[MARKERS]["Titel"] =$row["title"];
	    $node[MARKERS]["Diplom"] = .......usw....
            //........usw.......
            $items[] = $node;
   }
   $topnode[REGIONS]["items"] = $items;
   if(!$cut){
       $topnode[MARKERS]["irgendetwas"]="Was weiss ich";
       $topnode[CUTTERS]["CUT_irgendwas"]=false;
   }else 
       $topnode[CUTTERS]["CUT_irgendwas"]=true;
   //replaceTemplate siehe unten
   $content = $this->replaceTemplate($topnode);
   return $this->pi_wrapInBaseClass($content);

OK, den CUTTER brauchste nicht, aber vielleicht doch und so gibt das unten sinn und ich muss es nicht reduzieren.
Jetz noch nützliche Unterfunktionen zur Templateverarbeitung mit denen du eigentlich alles erschlagen können solltest.

 function replaceTemplate($array, $tpl=""){
  	if(empty($tpl)){
    	if(in_array("TYPE", array_map("strtoupper", array_keys($array)))){
		  	$out = $this->_doTemplateItem($array, $this->templateCode);
      }else{
		  	$out = "<p>Unecpected call of function replaceTemplate.</p>";
      }
    }else{
		  	$out = $this->_doTemplate($array, $tpl);
 		}
    return $out;
  }

  function _doTemplate($inReps, $tpl){
  	global $elmar;
    if(is_array($inReps)){
	    foreach($inReps as $k => $v){ $reps[strtolower($k)] = $v; }
	    $tpl = $this->_doTemplateRegions($reps[regions], $tpl);
	    $tpl = $this->_doTemplateMarkers($reps[markers], $tpl);
	    $tpl = $this->_doTemplateCutters($reps[cutters], $tpl);
    }
    return $tpl;
	}

  function _doTemplateRegions($containers, $tpl){
    $markers = array();
		if(is_array($containers)){
    	foreach($containers as $container => $items){
				$itemsTpl = $this->cObj->getSubpart($tpl, "###".$container."###");
        $markers[$container] = $this->_doTemplateItems($items, $itemsTpl);
      }
    }
  	$MARKERS = array();
		if(is_array($markers)){
    	foreach($markers as $k => $v) $MARKERS["###".$k."###"] = $v;
    }
    $out = $this->cObj->substituteMarkerArrayCached($tpl, array(), $MARKERS, array());
    return $out;
  }


  function _doTemplateItems($items, $itemsTpl){
    if(is_array($items)){
      foreach($items as $counter => $components){
        $out .= $this->_doTemplateItem($components, $itemsTpl);
      }
    }else{
			$out = $tpl;
    }
    return $out;
	}

  function _doTemplateItem($components, $itemsTpl){
    if(is_array($components)){
	    foreach($components as $k => $v){ $comps[strtolower($k)] = $v; }
	    $itemTpl = $this->cObj->getSubpart($itemsTpl, "###".$comps["type"]."###");
    }
    $out = $this->_doTemplate($comps, $itemTpl);
	  return $out;
  }

  function _doTemplateMarkers($markers, $tpl){
  	$MARKERS = array();
		if(is_array($markers)){
    	foreach($markers as $k => $v) $MARKERS["###".$k."###"] = $v;
    }
    $out = $this->cObj->substituteMarkerArrayCached($tpl, $MARKERS, array(),array());
    return $out;
  }

  function _doTemplateCutters($cuts, $tpl){
  	$clips = array();
    if(is_array($cuts)){
	    foreach($cuts as $k => $v){
	      $k = "###".$k."###";
	      $clips[$k] = $v ? "" : $this->cObj->getSubpart($tpl, $k);
	    }
    }
	return $this->cObj->substituteMarkerArrayCached($tpl, array(), $clips, array() );
  }

So, echt anstengend in diesem winzEditierFenster rumzumachen, ohne den Überblick zu verlieren.
Hoffe, das war hilfreich.
Gruß
Jens