Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting ContentType in a context filter does not work in a XML template #322

Closed
janchaloupka opened this issue Nov 22, 2022 · 1 comment
Closed

Comments

@janchaloupka
Copy link

Version: 3.0.4

Bug Description

I have encountered a problem when I was creating a custom filter for XML files that escapes content in a CDATA block.

The problem is that I need the Latte parser to treat the returned value of this filter as already escaped (safe) text. In a HTML template everything works as expected. however when I use this filter in a XML template (specified using a {contentType xml} block), the returned value is always escaped by Latte no matter the selected ContentType in the filter.

Steps To Reproduce

$latte = new \Latte\Engine();
$latte->addFilter('cdata', static function(\Latte\Runtime\FilterInfo $info, string $data) {
    $info->contentType = \Latte\ContentType::Xml; // Same result with any other ContentType
    return '<![CDATA[' . $data . ']]>';
});

$latte->setLoader(new \Latte\Loaders\StringLoader(['template' => '{contentType xml}<?xml version="1.0"?><root>{$foo|cdata}</root>']));
$latte->render('template', ['foo' => '<a>Hello world</a>']);

Output: <?xml version="1.0"?><root>&lt;![CDATA[&lt;a&gt;Hello world&lt;/a&gt;]]&gt;</root> (incorrect)

Expected: <?xml version="1.0"?><root><![CDATA[<a>Hello world</a>]]></root>

Expected Behavior

When used in a HTML template, everything works as expected. I would expect the same behaviour for XML templates.

$latte = new \Latte\Engine();
$latte->addFilter('cdata', static function(\Latte\Runtime\FilterInfo $info, string $data) {
    $info->contentType = \Latte\ContentType::Html;
    return '<![CDATA[' . $data . ']]>';
});

$latte->setLoader(new \Latte\Loaders\StringLoader(['template' => '<root>{$foo|cdata}</root>']));
$latte->render('template', ['foo' => '<a>Hello world</a>']); // This returns the expected result
@dg dg closed this as completed in 4aaa6e2 Jan 12, 2023
@dg
Copy link
Member

dg commented Jan 12, 2023

It should work, but you'll need to set $info->contentType = Latte\ContentType::Html; Sometime in the future I'll make it work better with Xml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants