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

Template aus Variable und nicht aus Datei
Goto page 1, 2  Next
 
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
wwl
Smarty Rookie


Joined: 29 Apr 2018
Posts: 9

PostPosted: Sun Apr 29, 2018 4:22 pm    Post subject: Template aus Variable und nicht aus Datei Reply with quote

Hallo, kann ich ein Template aus einer Variablen an Smarty übergeben?

Normal würde ich es aus einer tpl Datei auslesen und dann mit display ausgeben:
Code:
$smarty->display('test.tpl')

Ich habe aber den Inhalt einer Template in der Datenbank stehen und möchte diesen dann an Smarty übergeben.

Dazu finde ich nichts in der Dokumentation: Könnt ihr mir einen Tipp geben? Geht das?

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


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Mon Apr 30, 2018 7:59 am    Post subject: Reply with quote

Doch, in der Dokumentation ist es beschrieben, siehe hier: https://www.smarty.net/docs/en/resources.custom.tpl
Back to top
View user's profile Send private message Visit poster's website
wwl
Smarty Rookie


Joined: 29 Apr 2018
Posts: 9

PostPosted: Mon Apr 30, 2018 6:57 pm    Post subject: Reply with quote

Hallo,
ich hab Anhand der Dokumentation und Google folgendes gefunden. Aber es funktioniert leider nicht. Ich muss das Template aus einer Variable holen und wieder an eine Variable übergeben.
Code:

$source = "<h1>Hallo {$name}, herzlich Willkommen!</h1>";
$smarty = new Smarty();
$smarty->caching = false;
$smarty->assign('name','Christian');
$smarty->debugging = false;

$output = $smarty->fetch('string:'.$source);
print $output;

Ergebnis:
Hallo , herzlich Willkommen!
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Mon Apr 30, 2018 7:04 pm    Post subject: Reply with quote

Were you expecting the answer to be "press the button"?
You have to write your own plugin (that is, a code), which does what you need to do.
Luckily for you, 99% of the work has been done already in the very documentation which you claimed you read, but apparently did not.
Back to top
View user's profile Send private message
wwl
Smarty Rookie


Joined: 29 Apr 2018
Posts: 9

PostPosted: Mon Apr 30, 2018 7:37 pm    Post subject: Reply with quote

Ok, but then I can not find it. Can you tell me where that is what I need?
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Tue May 01, 2018 6:10 am    Post subject: Reply with quote

https://www.smarty.net/forums/viewtopic.php?p=93273#93273
Back to top
View user's profile Send private message
wwl
Smarty Rookie


Joined: 29 Apr 2018
Posts: 9

PostPosted: Tue May 01, 2018 8:43 pm    Post subject: Reply with quote

Weis niemand eine Lösung für mein Problem?
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Wed May 02, 2018 5:28 am    Post subject: Reply with quote

Vielleicht solltest du die "string"-Resource vorher registrieren?

So wie in der Doku beschrieben?

Code:
$smarty = new Smarty();
$smarty->registerResource('mysql', new Smarty_Resource_Mysql());

// using resource from php script
$smarty->display("mysql:index.tpl");
Back to top
View user's profile Send private message Visit poster's website
wwl
Smarty Rookie


Joined: 29 Apr 2018
Posts: 9

PostPosted: Thu May 03, 2018 11:14 am    Post subject: Reply with quote

Ich bin mir nicht sicher ob ich mich richtig Ausgedrückt habe.

Ich habe ein kompletts html Template mit allen Marker in einer PHP Variable stehen - will das dann durch Smarty schicken - und dann wieder in eine Variable ausgeben.

Der letzte Teil funktioniert durch fetch. Aber wie bekomme ich die Template Variable erst mal in Smarty rein. Ich hab mir das mit den String Resourcen schon angeschaut. Die basieren aber darin das ich die Daten aus einer Datenbank auslese.
Gibt es denn eine einfache Methode die Template Variable an Smarty zu übergeben. Also nochmal: die Template Variable beinhaltet ein gesemtes Template, nicht nur ein paar Variable.

Besten Dank für Hilfe
Christian
Back to top
View user's profile Send private message
Grizzly
Smarty Pro


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Thu May 03, 2018 11:25 am    Post subject: Reply with quote

Okay, lass mich zusammenfassen wie ich es verstehe:

Du hast ein template, zum Beispiel:

Code:
Hallo hier steht der Text aus der Variable: {$Test}


Der "Inhalt" von der Variable ist dann sowas wie:

Code:
Hallo, dies ist ein Test und es soll variabble a ausgegeben werden: {$a}


Und du willst, dass deine Variable a nicht als String ausgegeben wird sondern der Wert von a, richtig???
Back to top
View user's profile Send private message Visit poster's website
wwl
Smarty Rookie


Joined: 29 Apr 2018
Posts: 9

PostPosted: Thu May 03, 2018 12:01 pm    Post subject: Reply with quote

Nee?
Nicht so kompliziert:

Ich habe eine Variable ($template)

$template = "Hier ist eine gesamte HTML Seite inclusive verschiedener Smarty Marker drin {$vorname}{$name}.";

Dieses $template möchte ich jetzt an Smarty übergeben. Also keine Template Datei, sondern den Inhalt der Variable.

Smarty ersetzt dann die Marker mittels assign (usw.) und gibt mir dann die komplette Seite per fetch wieder an eine Variable zurück.

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


Joined: 15 Apr 2011
Posts: 172
Location: Germany

PostPosted: Fri May 04, 2018 5:23 am    Post subject: Reply with quote

Hä? Dann verstehe ich aber nicht was du willst. Es ist doch oben bereits von dir selbst beschrieben dass du eine Resource "string" hast

Code:
$output = $smarty->fetch('string:'.$source);



Dazu musst du deine Resource aber vorher registrieren, so wie in der Dokumentation beschrieben...
Back to top
View user's profile Send private message Visit poster's website
wwl
Smarty Rookie


Joined: 29 Apr 2018
Posts: 9

PostPosted: Sat May 05, 2018 10:06 am    Post subject: Reply with quote

Ja, ich hab es ja so gemacht, aber es funktioniert nicht.
Code:
$source = "<h1>Hallo {$name}, herzlich Willkommen!</h1>";

$smarty = new Smarty();
$smarty->registerResource('string:'.$source);

$smarty->caching = false;
$smarty->assign('name','Christian');

$output = $smarty->fetch('string:'.$source);

print $output;

$smarty->display('string:',$source);


Es kommt aber nur
Hallo , herzlich Willkommen!
raus.

Was mache ich falsch?
Christian
[/code]
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Sat May 05, 2018 1:29 pm    Post subject: Reply with quote

"string" is already registered by Smarty.
Back to top
View user's profile Send private message
wwl
Smarty Rookie


Joined: 29 Apr 2018
Posts: 9

PostPosted: Sat May 05, 2018 9:36 pm    Post subject: Reply with quote

If you know how it works, why do not you tell me?
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
Goto page 1, 2  Next
Page 1 of 2

 
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