Skip to content

Returning Html object from a filter causes a type error on anon blocks #407

@Crell

Description

@Crell

Version: 3.1.1

Bug Description

I have written a small extension as part of a larger project, which adds a custom filter that passes text through the CommonMark markdown library. Mostly it works.

However, after leaving the project alone for a while, I am now getting an error in a very specific situation.

Steps To Reproduce

// My extension, abridged...

class CommonMarkExtension extends Extension
{
    public function __construct(
        private readonly ConverterInterface $converter = new GithubFlavoredMarkdownConverter(),
    ) {}

    public function getFilters(): array {
        return [
            'markdown' => $this->markdownFilter(...),
        ];
    }

    public function markdownFilter(FilterInfo $info, string $s): Html {
        if ($s === '') {
            return new Html($s);
        }
        return new Html($this->converter->convert($this->dedent($s)));
    }

    private function dedent(string $str): string { ... }
}

My template:

{var $mdItalic = "*something* is working"}

{$md| markdown}

{='# Title here'| markdown}

{block| markdown}
    * This
    * should
    * work
{/block}

Expected Behavior

I would expect all 3 uses of the filter to work and generate HTML. However, while the first two work fine, the third (the anonymous block) throws an exception:

TypeError

Latte\Runtime\Helpers::convertTo(): Argument #3 ($s) must be of type string, Latte\Runtime\Html given, called in /app/cache/latte/root-routes-latte-test.latte--4412791dc1.php on line 90

I've spent the last 2 hours trying to debug this, and haven't been able to figure out where it goes wrong. Something is not working right, clearly, but from the documentation I would expect it to be. Returning a raw string from the filter removes the error, but of course then all of my output gets double-escaped.

Is this a bug in Latte or in my filter code?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions