簡體   English   中英

將TWIG輸出發送到Symfony中的zip進行模板化

[英]Sending TWIG output to a zip in Symfony for Templating

我需要創建一個能夠基於一組選項(包括菜單1,使用小頁腳等)輸出模板(根據需要包含所有js,css,img等文件的index.html的zip文件)的系統。

因此,我可以放入代碼塊,這樣實際上就不會很難將所需的外觀輸出到瀏覽器,但是如果我想保存渲染的html會怎樣(這意味着我們需要更改js等文件的位置)改成拉鏈?

有任何想法嗎? 目前,我認為無論如何都需要使用PHP(控制器)進行處理,而TWIG可能並沒有太大幫助,但這會使它變得更加復雜。

所以我解決了這個問題,這里是控制器,用於輸出所需的壓縮過的樹枝:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    /**
     * @Route("/{variableName}", name="homepage"})
     */
    public function indexAction($variableName = "Default")
    {
        //do whatever you need to get the template correct

        //send rendered TWIG to a variable
        $renderedhtml = $this->render('default/index.html.twig', [
            'variableName' => $variableName
        ])->getContent();

        $zipName = "template.zip";
        $main = $this->get('kernel')->getRootDir() . '/../web/workingdir/';
        $vername = time(); //we use the exact time to get a unique dir name

        //remove any projects older than a few minutes (5 in this case)
        $dirHandle = opendir($main);
        while($item = readdir($dirHandle)){
            if(is_dir($main . $item) && $item != '.' && $item != '..') {
                if ( ( ( $item + ( 60 * 5 ) ) < $vername ) ) {
                    system('/bin/rm -rf ' . escapeshellarg($main . $item));
                }
            }
        }

        //make the project dir and populate it with files (in this case just the template as index.html, you would want to copy all other files here though)
        mkdir($main.$vername);
        file_put_contents($main.$vername."/index.html", $renderedhtml);

        //create the zip
        $zip = new \ZipArchive();
        $zip->open($zipName,  \ZipArchive::CREATE);
        $zip = $this->createZip($zip, $main.$vername);
        $zip->close();

        //get the zip ready to return
        header('Content-Type', 'application/zip');
        header('Content-disposition: attachment; filename="' . $zipName . '"');
        header('Content-Length: ' . filesize($zipName));
        readfile($zipName);
    }

    // Create a zip with zip object and root dir params (find and include sub-dirs with content)
    private function createZip($zip, $source) {
        $source = str_replace('\\', '/', realpath($source));

        if (is_dir($source) === true) {
            $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST);

            foreach ($files as $file) {
                $file = str_replace('\\', '/', $file);

                if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
                continue;

                $file = realpath($file);

                if (is_dir($file) === true)
                {
                    $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                }
                else if (is_file($file) === true)
                {
                    $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                }
            }
        }
        return $zip;
    }
}

一旦進入此階段,您只需要將其他所有必需的文件添加到工作目錄中即可,但是我認為可以在其他問題下完成,這是將網站轉換為zip格式的html。

希望這對某人有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM