Feature #46257

Add escape sequence support for Fluid

Added by Rafael Kähm over 2 years ago. Updated 6 months ago.

Status:Under Review Start date:2013-03-14
Priority:Should have Due date:
Assigned To:- % Done:

0%

Category:Core
Target version:-
Has patch:No

Description

In some cases you need to print some characters to rendered view, which Fluid uses.
f.e.: {{object}} should output {value} but Fluid renders this to {{object}}

i have patch for this, which make it possible following possible:
\{{object}\} renders to {value}
\\\{{object}\\\} renders to \{value\}

please make gerrit from following gist:
https://gist.github.com/RafaelKa/5159642

there are 54,55,56 , 119-123 and 455,456 lines interesting

History

#1 Updated by Rafael Kähm over 2 years ago

yes it is a copy of CDATA but most convenient:

CDATA:
<![CDATA[{]]>{object}<![CDATA[}]]> renders to {value}

vs

escape sequenze:
\{{object}\} renders to {value}

#2 Updated by Bastian Waidelich over 2 years ago

  • Category set to Core
  • Status changed from New to Needs Feedback

To be honest, I'm not so sure that we should adjust the parser for this. Wouldn't

1&#123;{object}&#125;


work in your case?

#3 Updated by Rafael Kähm over 2 years ago

Bastian SGML/HTML/XML entity will not work
output is

&#123;value&#125;

and i think this is good so as it is -> maybe you need to print SGML/HTML/XML to

Escape sequence is usefull for StandaloneView.
Here is a demo
1 /** 2 * An example command to render views with escape sequenze 3 * 4 * This renders a view with Fluid to String and print this to your shell 5 * 6 * @return void 7 */ 8 public function escapeSequenzeDemoCommand() { 9 10 $templateSource = ' 11cdata <![CDATA[{]]>{object.property1}<![CDATA[}]]> 12escapeSequence \{{object.property1}\} 13 <!\[CDATA[\{]]>{object.property1}<!\[CDATA[\}]] 14 <!\[CDATA[{]]>{object.property1}<!\[CDATA[}]] 15XML entity &#123;{object.property1}&#125; 16'; 17 18 $view = new \TYPO3\Fluid\View\StandaloneView(); 19 $view->setTemplateSource($templateSource); 20 21 $object = array('property1' => 'value'); 22 $view->assign('object', $object); 23 echo $view->render() . PHP_EOL; 24 25 }

output:

1 cdata {value} 2 escapeSequence {value} 3 <![CDATA[{]]>value<![CDATA[}]] 4 <![CDATA[{]]>value<![CDATA[}]] 5 XML entity &#123;value&#125;

#4 Updated by Adrian Föder over 2 years ago

TBH I just discovered another use case for my very own, I needed to

1<f:form.hidden name="message[recipient]" value="{recipientIdentifier}" />

while the value should literally be {recipientIdentifier} (it's for JavaScript substitution lateron).
Any ideas how to achieve this currently?

#5 Updated by Rafael Kähm over 2 years ago

Adrian Föder wrote:

TBH I just discovered another use case for my very own, I needed to

[...]

while the value should literally be {recipientIdentifier} (it's for JavaScript substitution lateron).
Any ideas how to achieve this currently?


currently you can use CDATA

1<f:form.hidden name="message[recipient]" value="<![CDATA[{recipientIdentifier}]]>" />

i tried your example with my solution of escape sequenze as follows:

1<f:form.hidden name="message[recipient]" value="\{recipientIdentifier\} />

and Fluid prints:

1<input type="hidden" name="message[recipient]" value="{recipientIdentifier}" />

#6 Updated by Rafael Kähm over 2 years ago

with on https://gist.github.com/RafaelKa/5159642 code kickstarter breaks

1... 2use TYPO3\Flow\Annotations as Flow; 3...

to

1... 2use TYPO3FlowAnnotations as Flow; 3...

in
https://gist.github.com/RafaelKa/5232405

i added a list of to escaped characters [ ] { } \

are there some more characters to be escaped by Fluid?

#7 Updated by Tarcisio Fedrizzi over 2 years ago

Rafael Kähm wrote:

with on https://gist.github.com/RafaelKa/5159642 code kickstarter breaks

1... 2> use TYPO3\Flow\Annotations as Flow; 3> ...

to

1... 2> use TYPO3FlowAnnotations as Flow; 3> ...

in
https://gist.github.com/RafaelKa/5232405

i added a list of to escaped characters [ ] { } \

are there some more characters to be escaped by Fluid?

This works as well (I know that is not so clean but):

In the controller:

$this->view->assign('oBrc', '{');
$this->view->assign('cBrc', '}');

In the view:

{oBrc}MyValue{cBrc}

you can write the literal closing brace and you obtain the same value. Like this:

{oBrc}MyValue}

The use case that Adrian points out can be worked out in a simpler way like this:
controller:

$this->view->assign('recipientIdentifier', '{recipientIdentifier}');

view:

<f:form.hidden name="message[recipient]" value="{recipientIdentifier}" />

Of course if you have to print out many literal values (or to produce for example a JSON template) the ability to escape braces would be the best way to achieve it.

#8 Updated by Artus Kolanowski almost 2 years ago

Tarcisio Fedrizzi wrote:

This works as well (I know that is not so clean but):

In the controller:
[...]

In the view:
[...]

you can write the literal closing brace and you obtain the same value. Like this:

[...]

The use case that Adrian points out can be worked out in a simpler way like this:
controller:
[...]

view:
[...]

Of course if you have to print out many literal values (or to produce for example a JSON template) the ability to escape braces would be the best way to achieve it.

I think this is currently the most usable workaround. But instead of using a controller you can also use f:alias in a template.

<f:alias map="{ocb: '{', ccb: '}'}">
    <f:uri.action arguments="{start: '{ocb}start{ccb}'}" [...] />
</f:alias>

Anyway such a feature would make some templates much more cleaner.

#9 Updated by Gerrit Code Review almost 2 years ago

  • Status changed from Needs Feedback to Under Review

Patch set 1 for branch master has been pushed to the review server.
It is available at https://review.typo3.org/24898

#10 Updated by Hannes Lau 12 months ago

To my eyes, it would be better to escape single { characters, so one can still use Fluid variables inside a pair of escaped curly braces.

As an example, I am trying to use Fluid to produce some CSS code like this:

<n:headerData>
    <style type="text/css">
        .article-{newsItem.uid} \{
            background-image: url('{f:uri.image(src: "{mediaElement.uid}")}');
        \}
    </style>
</n:headerData>

This would not work with the \{escaped} approach.

#11 Updated by Stefan Neufeind 6 months ago

@Hannes: An escaped opening brace \{ can immediately be followed by another opening brace (for the variable). So imho you could do: \{{variable}}
It could be discussed whether escaping the closing brace as well would make it easier to read. But it's not mandatory since with no active opening brace at that point the closing brace is just regarded as text.

Also available in: Atom PDF