Images linked in db not displayed

Notice: This thread is very old.
jarjar
Member | 4
+
0
-

I am a begginner starting with new project (Nette 2.4). There is a post saved in db that contains a html tag, but the image is not displayed, although I use |noescape.

The image link iteself is working, it is displayed properly when inserted directly into template.

Could anybody please help? Thanks a lot

petr.jirous
Member | 128
+
0
-

how is your template rendered? paste final html please

jarjar
Member | 4
+
0
-

Template:

{block content}
<div class=“date”>{$post->created_at|date:‘%d. %m. %Y’}</div>
<div class=“post”>{$post->content|noescape}</div>
{/block}

Image contained in a post is located at:
{$basePath}/gallery/posts/test.jpg

I'm using the basic app mentioned here
Tracy enabled.

Last edited by jarjar (2016-12-31 11:42)

CZechBoY
Member | 3598
+
0
-

This is latte, not html. For example open source from web browser.

jarjar
Member | 4
+
0
-

Solved. Works better without using {$basePath}/…

David Kregl
Member | 52
+
+2
-

You definitely should use {$basePath} until you got some filters, which always return you the right path. Because even if it seems to work now, later, on the production server or under different circumstances it might not work as expected.

Always try to come up with a more general solution.

A class with the filter could look like this:

<?php

namespace AppBundle\Model\Filters;

use Nette\Http\IRequest;

class ImagePath
{
    /**
     * @var string
     */
    private $wwwDir;

    /**
     * ImagePath constructor.
     * @param IRequest $httpRequest
     */
    public function __construct(IRequest $httpRequest)
    {
        $this->wwwDir = $httpRequest->getUrl()->getBasePath();
    }

    public function __invoke($imageName)
    {
        return $this->wwwDir . '/uploads/photos/original/' . $imageName;
    }
}

This is how you register a new filter in Presenter (usually BasePresenter)

protected function beforeRender()
{
	parent::beforeRender();
	$template->addFilter('imagePath', new ImagePath($this->getHttpRequest()));
}

And this is how you use it in Latte

<img src="{$post->image|imagePath}" alt"{$post->title}">

Last edited by David Kregl (2017-01-04 06:53)

dkorpar
Member | 108
+
0
-

@DavidKregl
I'm actually a fan of not using $basePath at all, I just assume all websites I'm building will be on top level so code is actually cleaner (I like better src=“/dir/file.ext” then src=“{$basePath}dir/file.ext”)
I really don't see a problem my website not running in some subdirectory, in the end you can always really easy create subdomain…

$basePath caused much issues when updating nette in past (it was BC at one point) and I really don't see a point of using it

Last edited by dkorpar (2017-01-05 22:39)