Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Smarty-Problem - Anfänger braucht Hilfe

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German
View previous topic :: View next topic  
Author Message
Eistee
Smarty Rookie


Joined: 18 Jun 2004
Posts: 10

PostPosted: Fri Jun 18, 2004 9:50 am    Post subject: Smarty-Problem - Anfänger braucht Hilfe Reply with quote

Hallo, ich probiere mich grad darin, meine Seite auf smarty umzustricken.
Mitlerweile kan ich auch schon erste Erfolge verzeichnen Wink
Allerdings macht mir ein News-Syste noch immer größere Probleme.
Zunächst mal der Code meiner index.php:

[php:1:3697434e76]
<?php
define('SMARTY_DIR', '/is/htdocs/47449/www.alkbrueder.de/smarty_alkbrueder/smarty/');

require(SMARTY_DIR.'Smarty.class.php');

$tpl_ct = new smarty();
$tpl = new smarty();

include('thwbnews.php');
newstpl($tpl_ct);
$tpl->assign_by_ref('ct', $tpl_ct->fetch('templates/newsrow.html'));

$tpl->display('templates/index.html');
?>
[/php:1:3697434e76]

Nun ein Code-Ausschnitt aus der thwbnews.php, wie ich ihn bisher verändert habe:

[php:1:3697434e76]
while( list(, $thread) = each($a_thread) )
{
if( $maxchars && strlen($thread['posttext']) > $maxchars )
{
$newtext = substr($thread['posttext'], 0, $maxchars);
for( $i = $maxchars; $i < strlen($thread['posttext']); $i++ )
{
if( $thread['posttext'][$i] == ' ' || $thread['posttext'][$i] == "\n" )
break;
$newtext .= $thread['posttext'][$i];
}
$more = '<a href="'.$thwbpath.'showtopic.php?threadid='.$thread['threadid'].'"><img src="templates/images/for.gif" border="0" align="right"></a>';
$thread['posttext'] = $newtext;
}
else
{
$more = '<a href="'.$thwbpath.'showtopic.php?threadid='.$thread['threadid'].'"><img src="templates/images/for.gif" border="0" align="right"></a>';
}

$tpl_ct->assign('topic', parse_code($thread['threadtopic']));
$tpl_ct->assign('date', date($date_format, $thread['threadcreationtime']));
$tpl_ct->assign('author', $thread['threadauthor']);
$tpl_ct->assign('more', $more);
$tpl_ct->assign('text', parse_code($thread['posttext'], 1, 1, 1, 1));
}
[/php:1:3697434e76]

Nun der selbe Teil, wie er im Original aussieht:

[php:1:3697434e76]
$tpl_newsrow = file_get_contents("content/newsrow.html");

...

$news_output = '';
while( list(, $thread) = each($a_thread) )
{
if( $maxchars && strlen($thread['posttext']) > $maxchars )
{
$newtext = substr($thread['posttext'], 0, $maxchars);
for( $i = $maxchars; $i < strlen($thread['posttext']); $i++ )
{
if( $thread['posttext'][$i] == ' ' || $thread['posttext'][$i] == "\n" )
break;
$newtext .= $thread['posttext'][$i];
}
$more = '<a href="'.$thwbpath.'showtopic.php?threadid='.$thread['threadid'].'"><img src="images/for.gif" border="0" align="right"></a>';
$thread['posttext'] = $newtext;
}
else
{
$more = '<a href="'.$thwbpath.'showtopic.php?threadid='.$thread['threadid'].'"><img src="images/for.gif" border="0" align="right"></a>';
}

$row = $tpl_newsrow;
$row = str_replace('{more}', $more, $row);
$row = str_replace('{topic}', parse_code($thread['threadtopic']), $row);
$row = str_replace('{text}', parse_code($thread['posttext'], 1, 1, 1, 1), $row);
$row = str_replace('{author}', $thread['threadauthor'], $row);
$row = str_replace('{date}', date($date_format, $thread['threadcreationtime']), $row);
$row = str_replace('{threadid}', $thread['threadid'], $row);
$row = str_replace('{commentcount}', $thread['threadreplies'], $row);


$news_output .= $row;
}

...

print $news_output;
[/php:1:3697434e76]

Bei meiner abgeänderten Version besteht der Fehler darin, dass nur ein Eintrag aus DB gelesen und später ausgegeben wird. Das liegt daran, dass ich, nicht wie im Original, alle Einträge zunächst in der Variable $news_output speichern und später ausgeben kann.
Wie ist es dennoch möglich, alle Einträge per smarty auszugeben?
Wäre nett, wen ihr mir einen Lösungsweg konkret an diesem Beispiel erklären könntet, da ich mich erst seit wenigen Tagen überhaupt mit dieser Template-Engine beschäftige.
Danke!
Back to top
View user's profile Send private message
noftinator
Smarty n00b


Joined: 26 Jun 2004
Posts: 3

PostPosted: Sun Jun 27, 2004 12:08 am    Post subject: Re: Smarty-Problem - Anfänger braucht Hilfe Reply with quote

Hi,

hmm hoffe ich habe Dein Problem richtig verstanden, denke aber schon. Das Problem ist meiner Meinung nach, dass Du noch zuviel mit PHP machst. Hoffe mein Posting hilft Dir weiter!

Quote:

Nun ein Code-Ausschnitt aus der thwbnews.php, wie ich ihn bisher verändert habe:

[php:1:b0ab9dafe1]
while( list(, $thread) = each($a_thread) )
{
// SCHNIPP
$tpl_ct->assign('topic', parse_code($thread['threadtopic']));
$tpl_ct->assign('date', date($date_format, $thread['threadcreationtime']));
$tpl_ct->assign('author', $thread['threadauthor']);
$tpl_ct->assign('more', $more);
$tpl_ct->assign('text', parse_code($thread['posttext'], 1, 1, 1, 1));
}
[/php:1:b0ab9dafe1]


Wow... habe Deinen Code erstmal aufs wesentliche gekürzt, soweit ich das verstanden habe übergibst Du hier die News an die Smarty-Instanz.

Quote:

Bei meiner abgeänderten Version besteht der Fehler darin, dass nur ein Eintrag aus DB gelesen und später ausgegeben wird.


Und hier liegt auch schon das Problem: Mit obigem Code überschreibst Du wohl jedes mal den Inhalt, den Du beim vorherigen Durchlauf der while-Schleife zugewiesen hast.

Quote:

Das liegt daran, dass ich, nicht wie im Original, alle Einträge zunächst in der Variable $news_output speichern und später ausgeben kann.
Wie ist es dennoch möglich, alle Einträge per smarty auszugeben?


Ja, dazu baust Du ein Array mit allen Deinen News, die angezeigt werden sollen. Diese zeigst Du dann mit einer Section an.

Quote:

Wäre nett, wen ihr mir einen Lösungsweg konkret an diesem Beispiel erklären könntet, da ich mich erst seit wenigen Tagen überhaupt mit dieser Template-Engine beschäftige.
Danke!


Also, hier ein Beispiel-Array (gekürzt).

[php:1:b0ab9dafe1]
// Gekürztes Array-Beispiel
$aMeineNews = array(
array(
'topic' => 'Ganz frisch passiert!',
'date' => '20.06.2004',
'author' => 'Sabine Mustermann'
),
array(
'topic' => 'Schon bischen älter',
'date' => '19.06.2004',
'author' => 'Mein Name'
)
);

// An Smarty geben
$oSmarty->assign('NEWS_CONTENT', $aMeineNews);
[/php:1:b0ab9dafe1]

So... und nun baust Du in Dein Template eine Section ein, mit der Du die News ausgibst.

Code:

{ section name=newsbeitraege loop=$NEWS_CONTENT }
  <B>News-Beitrag</B><BR><BR>
  Topic: { $NEWS_CONTENT[newsbeitraege].topic }<BR>
  Autor: { $NEWS_CONTENT[newsbeitraege].author }<BR>
  Datum: { $NEWS_CONTENT[newsbeitraege].date }<BR>
{ /section }


Dann hast Du die ganze Formatierungs-Arbeit auch nicht in PHP zu erledigen - dafür gibts ja Smarty Wink

Links dazu:

http://smarty.php.net/crashcourse.php
http://smarty.php.net/manual/en/language.function.section.php

Gruss,

Thorsten
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Language: German All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP