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

php in .tpl

 
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
klausi89
Smarty n00b


Joined: 13 Nov 2016
Posts: 1

PostPosted: Sun Nov 13, 2016 12:08 am    Post subject: php in .tpl Reply with quote

Hallo ich bin völlig neu im Umgang mit Smarty

und zwar möchte ich gerne meine Navbar beibehalten.

Diese ist nach diesem Schema aufgebaut:

if($link1 == "on"){
echo '<li class="activated"><a href="/link1/index.php">Bitcoin</a></li>';
}
else {
echo '<li class="activated"><a href="/">Link1</a></li>';
}
if($link2 == "on"){
echo '<li class="activated"><a href="/link2/index.php">Link2</a></li>';
}
else {
echo '<li class="activated"><a href="/">Link2</a></li>';
}

in meiner .tpl

Datei hab ich nur {$navbar} rein

allerdings bekomme ich das nicht hin das es auch angezeigt wird.

assign , display ist mir im moment noch etwas schleierhaft.....
Hoffe es könnte mir bitte mal jemand helfen

Gruß
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Tue Nov 15, 2016 7:14 am    Post subject: Reply with quote

Hallo,

erstmals, willkommen im Forum.
Ich würde dich bitten den Code-Tag des Forums zu verwenden, da man das ganze sonst schlecht lesen kann...

Nun zu deiner Frage. Wenn du Smarty verwendest, brauchst du kein echo zu verwenden. Alles was du in deiner PHP-Datei gemacht hast, kannst nun auch direkt in der TPL-Datei machen.

Display bedeutet so viel wie "Zeige/Lade/Rendere mir folgende TPL-Datei"
Assign bedeutet (wo wie es heißt) zuweisung einer Variable und ist eben mit dem folgendem PHP Code zu verstehen

Code:
$a = $b


Alles was du in die Assign Methode einfügst wird dann im Template als Variable zur Verfügung gestellt. Bei dir müsstest du also deine Variablen ($link1 und $link2) am besten in Arrays speichern (oder etwas ähnlichem) und dann mit einer [url='http://www.smarty.net/docs/en/language.function.foreach.tpl']foreach[/url] das ganze durchgehen. Dabei musst du nur in die TPL dein <li> eingeben.

Beispiel (ungetestet):

PHP Code:


Code:

$smarty->assign('Links', array(
                             array('Url' => '/link1/index.php',
                                   'Title' => 'Bitcoin'),
                             array('Url' => '/link2/index.php',
                                   'Title' => 'Link2')
                             ));


Dann machst du eben dein Display:

Code:
$smarty->display("DeinTplFile.tpl");


und dann steht dir in deiner Template Datei die Variable "Links" (siehe oben in assign-Methode) zur Verfügung. In dein Template schmeißt du nun folgendes rein:

Code:

<ul>
{foreach $Links as $linkItem}
    <li class="activated"><a href="{$linkItem.Url}">{$linkItem.Title}</li>
{/foreach}
</ul>
Back to top
View user's profile Send private message Visit poster's website
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