<?php

class Render
{
  public $loader;
  public $twig;
  public $pageInfo;
  public $menu;
  public $background;
  public function __construct()
  {
    $config = new Settings();
    $this->loader = new \Twig\Loader\FilesystemLoader("../content/themes");
    $this->twig = new \Twig\Environment($this->loader, []);
    $settings = $config->getSettings();
    $this->menu = $settings["menu"];
    $this->pageInfo = [
      "keywords" => $settings["global"]["keywords"],
      "description" => $settings["global"]["descriptions"],
      "image" => $settings["global"]["background"],
    ];
  }

  public function renderTags()
  {
    $list = Sorting::tags();
    foreach ($list as $item) {
      $template = "fipamo-default/tags.twig";
      $pageOptions = [
        "title" => "Pages Tagged as " . $item["tag_name"],
        "background" => $this->pageInfo["image"],
        "tag_list" => $item["pages"],
        "info" => $this->pageInfo,
        "menu" => $this->menu,
      ];

      $html = $this->twig->render($template, $pageOptions);

      $location = "../public/tags/" . $item["slug"] . ".html";
      if (!is_file($location)) {
        file_put_contents($location, $html);
      } else {
        ($new = fopen($location, "w")) or die("Unable to open file!");
        fwrite($new, $html);
        fclose($new);
      }
    }
  }
}