Content

Verze:

18. 07. 2025

Zodpovědná osoba:

Dominik Šlechta

Poslední aktualizace:

18. 07. 2025, kocandajan488@gmail.com

Code

<?php

namespace Filters;

use App\Presenters\Front\BasePresenter;
use App\Repository\GalleryRepository;
use App\Repository\MenuRepository;
use App\Repository\ProductRepository;
use Nette;
use Nette\Application\BadRequestException;
use Nette\Application\UI\TemplateFactory;
use Nette\Utils\Html;

class Content
{
    use Nette\SmartObject;

    private $menuRepository;

    private $productRepository;

    private $galleryRepository;

    private $latte;

    public function __construct(MenuRepository $menuRepository, ProductRepository $productRepository, GalleryRepository $galleryRepository, TemplateFactory $latte)
    {
        $this->menuRepository = $menuRepository;

        $this->productRepository = $productRepository;

        $this->galleryRepository = $galleryRepository;

        $this->latte = $latte;
    }

    public function __invoke($page, $pure = false, $web = [], $lang = false, $generateWrapper = true)
    {
        $text = $page['text'];

        if ('' === $text) {
            return '';
        }

        if ($pure) {
            return $text;
        }

        if ($generateWrapper) {
            $content = Html::el("div class='content'");
            $content->setHtml($this->lookup($text, $web, $page, $lang));
            return $content->render();
        }

        return $this->lookup($text, $web, $page, $lang);

    }

    private function lookup($text, $web, $page, $lang)
    {
        return $text;
    }
}

Lookup function

To use correctly use any content includes call content like {$page|content,false,$web|noescape}

Copy following code of include you want to the lookup function. You can create your own if web or situation needs it

Anchor

Calling from content (without *):

  • {*ANCHOR:idOfAnchor} 
  • e.g. {*ANCHOR:how-to-create-variables}
// Content | Anchor
preg_match_all("/{ANCHOR:([A-Za-z-_\d]+)}/", $text, $anchors, PREG_SET_ORDER);

   if (count($anchors) > 0) {

      foreach ($anchors as $item) {

         try {
            if (isset($item[1])) {
               $html = $this->latte->createTemplate()->setFile(__DIR__ . "/content/anchor.latte");
               $html->anchorId = $item[1];
               $html->web = $web;
               $text = str_replace($item[0], $html, $text);
            }

         } catch (BadRequestException $e) {
            $text = str_replace($item[0], '', $text);
            continue;
         }
      }
   }      

 

Photogallery

Calling from content (without *)

  • {*PHOTOGALLERY-galleryId} 
  • e.g. {*PHOTOGALLERY-2}
preg_match_all('/{PHOTOGALLERY-(\d+):?(\d+)?}/', $text, $galleryItems, PREG_SET_ORDER);

   if (count($galleryItems) > 0) {
      foreach ($galleryItems as $item) {
         $galleryId = $item[1];
         //                $galleryShow =
         //                isset($item[2]) ?
         //                !empty($item[2]) ?
         //                (int)$item[2] : 4 : 4;
         try {
            $galleryRes = $this->galleryRepository->photosById($galleryId);

            if (count($galleryRes) > 0) {
               $html = $this->latte->createTemplate()->setFile(__DIR__ . '/content/gallery.latte');
               $html->photos = $galleryRes;
               $html->web = $web;
               $html->page = $page;
               $html->trigger = 'photos_' . $galleryId;
               $html->maxShow = 3;

               $text = str_replace($item[0], $html, $text);
            } else {
               $text = str_replace($item[0], '', $text);
            }
         } catch (BadRequestException $e) {
            $text = str_replace($item[0], '', $text);
            continue;
         }
      }   
   }

 

Banner

Calling from content (without *):

  • {*BANNER-title;perex;button;button-link}
  • e.g. {*BANNER-Contact us;You can submit a form with your questions or call us;Contact;/cs/m-3-contact}
preg_match_all('/{BANNER(?:-([a-zA-Zá-žÁ-Ž0-9.!:?,;\-\/ ]*?))?}/', $text, $info, PREG_SET_ORDER);
if (count($info)) {
   foreach ($info as $item) {
      $parts = isset($item[1]) ? explode(';', $item[1]) : false;

      $html = $this->latte->createTemplate()->setFile(__DIR__ . '/content/banner.latte');
      $html->parts = $parts;
      $html->web = $web;

      $text = str_replace($item[0], $html, $text);
   }
}

Blog

 

Product

 

Slider

Glide extension is needed for this

Calling from content (without *):

  • {*SLIDER-menuitemId;menuitem2Id;menuitem3Id;...}
  • e.g. {*SLIDER-24;25;28;34}
// Content | Slider
preg_match_all("/{SLIDER-([\d;]*?)}/", $text, $citace, PREG_SET_ORDER);
if (count($citace)) {
   foreach ($citace as $item) {
      $citaceIds = explode(';', $item[1]);
      $citaceArray = [];

      foreach ($citaceIds as $id) {
         try {
            $citaceArray[$id] = $this->menuRepository->page(intval($id));
         } catch (BadRequestException) {
            continue;
         }
      }

      $html = $this->latte->createTemplate()->setFile(__DIR__ . '/content/slider.latte');
      $html->item = $citaceArray;
      $html->web = $web;
      $text = str_replace($item[0], $html, $text);
   }
}

 

Newsletter

Calling from content (without *):

  • {*NEWSLETTER}
// Content | Newsletter
preg_match_all('/{NEWSLETTER}/', $text, $newsletters, PREG_SET_ORDER);
if (count($newsletters)) {
   foreach ($newsletters as $item) {
      try {
         $html = $this->latte->createTemplate()->setFile(__DIR__. '/content/newsletter.latte');
	 $html->web = $web;
         $text = str_replace($item[0], $html, $text);
      } catch (BadRequestException $e) {
	 $text = str_replace($item[0], '', $text);
         continue;
      }
   }
}