ulozenie template cez FileResponse

MKI-Miro
Člen | 261
+
0
-

Ahojte

mam takyto kod

$this->template->ephs = $this->is_ephRepository->findAll();
        $this->sendResponse(new \Nette\Application\Responses\FileResponse(__DIR__ . '/templates/Eph/csv.latte', "eph.csv"));

a csv.latte

Dobierka;Meno;Ulica;Mesto;PSC;Telefón;E-mail;Var symbol;Počet Valikov;Poistné
{foreach $ephs as $eph}
{$eph->order->price};{$eph->order->name};{$eph->order->street};{$eph->order->city};{$eph->order->zip};{$eph->order->phone|replace:" ":""};{$eph->order->mail};{$eph->order->variable};1;{if $eph->order->price == 0}20{else}{$eph->order->price}{/if}
{/foreach}

Problem je ze mi to nevrati realne data ale vrati mi to subor v ktorom je kod z latte

ako to opravit ?

dakujem

CZechBoY
Člen | 3608
+
0
-

Musel bys do tý šablony totiž vložit dat a renderovat ji. Takhle jen načteš soubor šablony a pošleš.
Podívej se třeba sem https://phpfashion.com/…lech-a-nette

MKI-Miro
Člen | 261
+
0
-

vdaka ale vobec mi z toho neje jasne ako nato

skusil som nieco take ale nefunguje

$template = $this->createTemplate();
        $template->setFile(__DIR__ . '/templates/Eph/csv.latte');

        $this->template->ephs = $this->is_ephRepository->findAll();
        $this->sendResponse(new \Nette\Application\Responses\FileResponse($template, "eph.csv"));
CZechBoY
Člen | 3608
+
0
-
$template->render()

nebo

(string)$template
MKI-Miro
Člen | 261
+
0
-

ak som to sprváne pochopil

$template = $this->createTemplate();
        $template->setFile(__DIR__ . '/templates/Eph/csv.latte');

        $this->template->ephs = $this->is_ephRepository->findAll();
        $this->sendResponse(new \Nette\Application\Responses\FileResponse($template->render(), "eph.csv"));

tak to nefunguje :( ani (string)$template

MKI-Miro
Člen | 261
+
0
-

nejake ine moznosti?

CZechBoY
Člen | 3608
+
0
-

A zkusil si ten (string)$template?

$template = $this->createTemplate();
$template->setFile(__DIR__ . '/templates/Eph/csv.latte');

$this->template->ephs = $this->is_ephRepository->findAll();
$this->sendResponse(new \Nette\Application\Responses\FileResponse((string)$template, "eph.csv"));
nightfish
Člen | 472
+
-1
-

FileResponse funguje tak, že vrátí obsah souboru, jehož jméno je předáno prvním parametrem. A protože šablona v $template žije jenom v paměti, nikoliv na disku, tak to takto jednoduše fungovat nebude.

Řešením je vyrenderovanou šablonu uložit do dočasného souboru, tento dočasný soubor poslat ve FileResponse a (čas od času) ty staré dočasné soubory promazat.

$tempDir = __DIR__.'/../temp/csv'; // existující adresář pro ukládání dočasných souborů

// vyčištění starých souborů - starších než 1 hodina (tento blok může být úplně mimo a spouštět se třeba cronem)
$filesToCleanup = glob($tempDir.'/csv*');
foreach ($filesToCleanup as $f) {
  if (filemtime($f) < Time() - 3600) {
    unlink($f);
  }
}
$template = $this->createTemplate();
$template->setFile(__DIR__ . '/templates/Eph/csv.latte');

$this->template->ephs = $this->is_ephRepository->findAll();
$fileName = tempnam($tempDir, 'csv');
file_put_contents($fileName, (string)$template);
$this->sendResponse(new \Nette\Application\Responses\FileResponse($fileName, "eph.csv"));
David Matějka
Moderator | 6445
+
+1
-

je lepsi si udelat treba nejakou content response https://gist.github.com/matej21/7975975