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

Varible als Template verwenden?

 
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
daniel987
Smarty Rookie


Joined: 26 Aug 2004
Posts: 20

PostPosted: Sun Sep 05, 2004 6:28 pm    Post subject: Varible als Template verwenden? Reply with quote

Da ich mein Templates aus eine Datenbank lade würde ich gerne smarty anweisen den Text einer Variable als Template zu verwenden wie bekomme ich sowas hin bsp.
_________________
It's better to burn out than to fade away.
Back to top
View user's profile Send private message
kills
Smarty Elite


Joined: 28 May 2004
Posts: 493

PostPosted: Mon Sep 06, 2004 6:49 am    Post subject: Re: Varible als Template verwenden? Reply with quote

daniel987 wrote:
Da ich mein Templates aus eine Datenbank lade würde ich gerne smarty anweisen den Text einer Variable als Template zu verwenden wie bekomme ich sowas hin bsp.


einfach den text der variable innerhalb der templates mit eval ausführen

TPL:
Code:

{ eval($sTemplate) }
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Sep 06, 2004 8:22 am    Post subject: Re: Varible als Template verwenden? Reply with quote

kills wrote:
Code:

{ eval($sTemplate) }


@kills: du pastest diesen code-schnippsel jetzt schon zum zweiten mal. was soll das? das da oben ist weder gültiges smarty nocht gültiges php. du meinst: {eval var=$sTemplate}

@daniel987: {eval} ist ganz ganz schlimm und böse. vermeide es zu benutzen. Für smarty-source aus der datenbank gibt es alternativ: http://smarty.php.net/manual/en/template.resources.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website
daniel987
Smarty Rookie


Joined: 26 Aug 2004
Posts: 20

PostPosted: Mon Sep 06, 2004 3:31 pm    Post subject: Reply with quote

Ich weis das eval böse ist Very Happy . Ich habe das. Auch das Manual habe ich schon zu Hilfe gezogen aber leider ist mein Problem dadurch nicht klar geworden.
Nach mehrmaligem ansehen zwar schon das meiste daran aber den letzen Schritt verstehe ich nicht nämlich:
Code:
// Ressource im PHP-Skript verwenden
      $smarty->display("db:index.tpl");

Woher kommt das index.tpl? Ich will doch Daten aus der Datenbank holen, nicht aus einer Datei.
Und noch etwas muss ich die abgefragen Datensätze immer in $tpl_source ablegen?
_________________
It's better to burn out than to fade away.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Sep 06, 2004 3:43 pm    Post subject: Reply with quote

daniel987 wrote:
Nach mehrmaligem ansehen zwar schon das meiste daran aber den letzen Schritt verstehe ich nicht nämlich:
Code:
// Ressource im PHP-Skript verwenden
      $smarty->display("db:index.tpl");

Woher kommt das index.tpl? Ich will doch Daten aus der Datenbank holen, nicht aus einer Datei.


das "index.tpl" ist das, was in deiner get_template()-funktion als $tpl_name übergeben wird.

du kannst auch: $smarty->display("db:123"); aufrufen. und in get_template dann irgendsowas wie:
Code:
query("SELECT * FROM templates WHERE id=" . (int)$tpl_name)


Quote:
Und noch etwas muss ich die abgefragen Datensätze immer in $tpl_source ablegen?


das muss so heissen wie der 2. parameter von get_template heisst. dieser parameter wird by-reference übergeben und da wird reingeschrieben.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
daniel987
Smarty Rookie


Joined: 26 Aug 2004
Posts: 20

PostPosted: Wed Sep 08, 2004 6:35 am    Post subject: Reply with quote

Ich habe nun eine Weile herumexpermentiert und komme nun nicht mehr weiter Smarty kann einfach die Ressource nicht lesen:
Code:
function db_get_template ($tpl_name, &$tpl_source, &$smarty_obj)
      {
      // Datenbankabfrage um unser Template zu laden,
      // und '$tpl_source' zuzuweisen
    
          $mysql=new mysql("localhost", "root", "");
$mysql->select_db("daniel-portal");
      $query=$mysql->query("SELECT Template FROM Test WHERE Section=".(int)$tpl_name);
     echo"$tpl_name";
      $temp_tpl_source = $mysql->get_results($query);
     $tpl_source=$temp_tpl_source['Template'];
      return;
     
      }
      function db_get_timestamp($tpl_name, &$tpl_timestamp, &$smarty_obj)
      {
     
      // Datenbankabfrage um '$tpl_timestamp' zuzuweisen
     $mysql=new mysql("localhost", "root", "");
$mysql->select_db("daniel-portal");
      $query=$mysql->query("SELECT Datum FROM Test WHERE Section=".$tpl_name);
     $tmp_tpl_timestamp=$mysql->get_results($query);
      $tpl_timestamp=$tmp_tpl_timestamp['Datum'];
    
     return true;
      }

Ich habe Testweis die sql abfragen mit "echo" ausgegeben, mit Erfolg.
Was kann denn da noch falsch sein? Als fehlermeldung erhalte ich immer:
Smarty error: unable to read resource: "db:1"
_________________
It's better to burn out than to fade away.
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Sep 08, 2004 7:05 am    Post subject: Reply with quote

db_get_template() muss true zurückliefern um smarty zu signalisieren, dass die resource geladen werden konnte und $tpl_source gültigen inhalt hat.

also
Code:
$tpl_source=$temp_tpl_source['Template'];
return true;

statt
Code:
$tpl_source=$temp_tpl_source['Template'];
return;
Back to top
View user's profile Send private message Send e-mail Visit poster's website
daniel987
Smarty Rookie


Joined: 26 Aug 2004
Posts: 20

PostPosted: Wed Sep 08, 2004 5:33 pm    Post subject: Reply with quote

Ich kann es nicht fassen jetzt funzt es tatsächlich vielen Dank.
_________________
It's better to burn out than to fade away.
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