contributte / menu-control

🍔 Menu and breadcrumb components for Nette framework (@nette)
MIT License
28 stars 20 forks source link

Error with latte filters #53

Closed darkWolf-PR closed 3 years ago

darkWolf-PR commented 3 years ago

Hi, after latest todays composer update, I´m getting this error while generating XML sitemap

Latte\RuntimeException Filters: unable to convert content type HTML to XML

It´s connected to this control in latte generating, at least it seems too-generated code (echo line is highlighted in tracy report):

$_tmp->renderSitemap();
$ʟ_fi = new LR\FilterInfo('html');
echo LR\Filters::convertTo($ʟ_fi, 'xml', ob_get_clean());

I´m pretty sure it worked before doing composer update, I´m using sitemap presenter, default.latte with this code(shortened-there some more xml stuff after menu-control added manually):

{contentType application/xml}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    {* Main pages *}
    {control menu:sitemap}
</urlset>

Any idea where is the problem?

foxycode commented 3 years ago

@darkWolf-PR Give me your composer.json please.

darkWolf-PR commented 3 years ago

@foxycode

{
    "name": "nette/web-project",
    "description": "Nette: Standard Web Project",
    "keywords": ["nette"],
    "type": "project",
    "license": ["MIT", "BSD-3-Clause", "GPL-2.0-only", "GPL-3.0-only"],
    "require": {
        "php": ">= 7.2",
        "ext-json": "*",
        "nette/application": "^3.0",
        "nette/bootstrap": "^3.0",
        "nette/caching": "^3.0",
        "nette/database": "^3.0",
        "nette/di": "^3.0",
        "nette/finder": "^2.5",
        "nette/forms": "^3.0",
        "nette/http": "^3.0",
        "nette/mail": "^3.0",
        "nette/robot-loader": "^3.0",
        "nette/security": "^3.0",
        "nette/utils": "^3.0",
        "latte/latte": "^2.5",
        "tracy/tracy": "^2.6",
        "ublaboo/datagrid": "^6.7",
        "ublaboo/datagrid-nette-database-data-source": "^2.0",
        "bicisteadm/webloader-reload": "^3.0",
        "scssphp/scssphp": "^1.0",
        "joseki/webloader-filters": "^1.1",
        "contributte/menu-control": "^2.2",
        "venca-x/nette-pagination": "dev-master",
        "ijvo/jquery-fileupload": "^2.1",
        "contributte/translation": "^0.8",
        "contributte/forms-bootstrap": "^0.3.2"
    },
    "require-dev": {
        "nette/tester": "^2.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app"
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}
foxycode commented 3 years ago

Works fine on my side with your version.

I believe you used own sitemap.latte on your side because original won't fit template you sent. Can you post your template?

And can you test using {contentType text/xml} in your template instead which seems to work fine?

darkWolf-PR commented 3 years ago

Yeah, I removed the xml headers from sitemap.latte - like this:

{varType Contributte\MenuControl\Menu $menu}
{varType Contributte\MenuControl\MenuItem $itemsParent}
{varType float $priority}
{define menu-branch, $itemsParent, $priority}
    {foreach $itemsParent->getItems() as $item}
    <url n:if="$item->isAllowed() && $item->isVisibleOnSitemap()">
        <loc>{$baseUrl}{$item->getRealLink()}</loc>
        <priority>{$priority}</priority>
    </url>
    {include menu-branch, $item, $priority - 0.1}
    {/foreach}
{/define}

{include menu-branch, $menu, 1}

I needed only menu-stuff generated from menu-control and add that to complete template (that default.latte) for xml . It´s because there are other sub-pages added(links to news and galleries as I can´t get these from menu-control...). Without removing xml related stuff, I would get that doubled in result...

Also, I´ve tried to change application/xml to text/xml - no change, same error.

foxycode commented 3 years ago

Ok, then problem is definitelly not in menu-control, because conversion is done outside of menu component :)

Try to render only direct output of {control menu:sitemap} and you will see if I'm right. You can also try to render your template without menu control an you will see if this still errors.

Let me know :)

darkWolf-PR commented 3 years ago

I´ve tried the second part already before - without menu-control and that works ok, all stuff correctly rendered in xml.

The menu-control is first, all my other stuff in that default template is after {control menu:sitemap}, I´ve removed it from code in 1st post because it works normally without menu-control.

darkWolf-PR commented 3 years ago

Ok, I´ve tried keeping only menu-control - like this:

{varType Nette\Utils\ArrayHash $news}
{varType Nette\Utils\ArrayHash $galleries}
{contentType text/xml}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    {* Main pages *}
    {control menu:sitemap}
</urlset>

Same error as before, so it has to be connected to menu-control. Again error on that line echo LR\Filters::convertTo($ʟ_fi, 'xml', ob_get_clean());

foxycode commented 3 years ago

I understand, but there is nu such error with default sitemap template. This error is probably related to latte. Try to render {control menu:sitemap} alone without any other content. Then paste html result directly instead of {control menu:sitemap} to your template. Let me know if error is still present.

darkWolf-PR commented 3 years ago

It runs without that error if I keep only {control menu:sitemap} in that template.

Gives me this list, whic is ok:

 <url>
        <loc>http://web.local/</loc>
        <priority>1</priority>
    </url>
    <url>
        <loc>http://web.local/about-us</loc>
        <priority>1</priority>
    </url>
    ...
    ...
darkWolf-PR commented 3 years ago

After few more test, it seems to work when I remove {contentType text/xml} from default.latte and can be kept in sitemap.latte. Sadly, I have no idea what going on with this...but it works without errors.

Browser shows header: Content-Type | text/xml;charset=UTF-8 so I guess I just leave it like this...

foxycode commented 3 years ago

This is definitely worth more attention. If you could create very simple script which will trigger this error without menu extension, you could report directly to latte. I believe this is something that should be fixed.

Or at least give me complete presenters and templates you're using so you I can trigger this.

darkWolf-PR commented 3 years ago

I tried these variants of adding {contentType text/xml}:

  1. nowhere = sitemap rendered - but type = HTML
  2. in default.latte, not in sitemap.latte = error
  3. in default.latte and in sitemap.latte = error
  4. in sitemap.latte, not in default.latte = sitemap rendered - type = XML

It always ends with error if it´s set in default.latte of my SitemapPresenter.

One think that came on my mind now - my SitemapPresenter extends BasePresenter (from Front module) and the menu-control is registered in this BasePresenter, so maybe the sitemap.latte template is "higher" in hierarchy?

Theres nothing else, my SitemapPresenter has only constructor getting 2 data repos and render method.

BasePresenter has menu-control

abstract class BasePresenter extends Nette\Application\UI\Presenter
{
   ...webloader, contribute translation, injected services for image cache and some other stuff...shoudl not be related to this case...

    /** @var IMenuComponentFactory $menuFactory */
    private $menuFactory;

    public function injectBasePresenter(IMenuComponentFactory $menuFactory)
    {
        $this->menuFactory = $menuFactory;
    }

    /**
     * @return MenuComponent
     */
    protected function createComponentMenu(): MenuComponent
    {
        return $this->menuFactory->create('front');
    }

SitemapPresenter

final class SitemapPresenter extends BasePresenter
{
    /** @var NewsRepository */
    private $newsRepo;

    /** @var GalleryRepository */
    private $galleryRepo;

    public function __construct(NewsRepository $newsRepo, GalleryRepository $galleryRepo)
    {
        parent::__construct();
        $this->newsRepo = $newsRepo;
        $this->galleryRepo = $galleryRepo;
    }

    public function renderDefault()
    {
        $this->template->news = $this->newsRepo->getAllNews();
        $this->template->galleries = $this->galleryRepo->getAll();
    }

Sitemap.latte

{varType Contributte\MenuControl\Menu $menu}
{varType Contributte\MenuControl\MenuItem $itemsParent}
{varType float $priority}
{contentType text/xml}
{define menu-branch, $itemsParent, $priority}
    {foreach $itemsParent->getItems() as $item}
    <url n:if="$item->isAllowed() && $item->isVisibleOnSitemap()">
        <loc>{$baseUrl}{$item->getRealLink()}</loc>
        <priority>{$priority}</priority>
    </url>
    {include menu-branch, $item, $priority - 0.1}
    {/foreach}
{/define}

{include menu-branch, $menu, 1}

SitemapPresenter - default.latte

{varType Nette\Utils\ArrayHash $news}
{varType Nette\Utils\ArrayHash $galleries}
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    {* Main pages *}
    {control menu:sitemap}
    {* News *}
    {capture $newsLink}{plink //:Front:News:default}{/capture}
    {foreach $news as $new}
    <url>
        <loc>{$newsLink}/{$new->id}-{$new->slug}</loc>
        <priority>0.5</priority>
    </url>
    {/foreach}
    {* Gallery *}
    {capture $galleryLink}{plink //:Front:Gallery:default}{/capture}
    {foreach $galleries as $gallery}
    <url>
        <loc>{$galleryLink}/{$gallery->id}-{$gallery->slug}</loc>
        <priority>0.5</priority>
    </url>
    {/foreach}
</urlset>

This is the working version. Should be everything that´s connected to this case.

darkWolf-PR commented 3 years ago

Hmm, I tried updating production version on active24 host and it doesnt setting the correct heeader here. It´s shown without error, but as text/html instead. While the same config on my localhost is showing header text/xml. Now, I have no idea what to do with it :-(

Edit: I´ve looked over nette forum and found other way to set xml - set responce in my SitemapPresenter:

    /** @var IResponse @inject */
    public $response;

    public function renderDefault()
    {
        $this->response->setContentType('text/xml');
        ...
     }

No collision with {contentType text/xml} in sitemap.latte, adding that to default.latte crashes app with same error as before.

foxycode commented 3 years ago

Since I'm not able to trigger this error and I believe it's latte error if it's not your templates, closing. Feel free to reopen if you could create minimalistic web-project like repository where this error could be triggered.