php - How to prevent escaping of a Twig Node Expression? -


i'm trying implement spread attributes in twig. i've got it, i'm not sure how prevent ouput being html-escaped.

i've registered new operator in twig_extension:

public function getoperators() {     return [         [/* unary operators */             '...' => array('precedence' => 10, 'class' => spreadattributes::class),         ],         [/* binary operators */             // ...         ]     ]; } 

my class looks this:

class spreadattributes extends twig_node_expression_unary {     public function compile(twig_compiler $compiler) {         $compiler             ->raw(\wxutility::class . '::html_attrs(')             ->subcompile($this->getnode('node'))             ->raw(')');     }      public function operator(twig_compiler $compiler) {         throw new \exception("unused");     } } 

usage:

<label{{ ...label_attrs }}> 

but compiled output looks this:

 echo twig_escape_filter($this->env, wxutility::html_attrs((isset($context["label_attrs"]) ? $context["label_attrs"] : $this->getcontext($context, "label_attrs"))), "html", null, true); 

i need rid of twig_escape_filter wxutility::html_attrs generates escaped html.

how can prevent escaping?


i think doable it's quite complicated. @ point in time compiler creates twig_node_print writes out echo statement. on line 73 of vendor/twig/twig/lib/twig/nodevisitor/escaper.php can see calls escapeprintnode in leavenode. escapeprintnode calls $node->getnode('expr') returns instance of our spreadattributes. that's passed off issafefor returns true if it's safe (doesn't need escaping). if it's not safe, applies twig_escape_filter.

so need override twig_nodevisitor_escaper::issafefor or twig_nodevisitor_safeanalysis::getsafe.

$twig->removeextension deprecated, can't remove default escaper extension. guess have override twig_environment take out of constructor. , there override getnodevisitors. , issafefor. twig_node_expression don't have useful properties can check; best can instanceof spreadattributes , that's major hack.

is there no better way?

not sure why didn't occur me sooner, took @ twig_escape_filter. won't escape string if it's instance of twig_markup. have wrap our output in that:

class spreadattributes extends twig_node_expression_unary {     public function compile(twig_compiler $compiler) {         $compiler             ->raw(__class__ . '::attrs(')             ->subcompile($this->getnode('node'))             ->raw(', $this->env->getcharset())'); // not sure if best way pass charset along, seems work     }      public function operator(twig_compiler $compiler) {         throw new \exception("unused");     }      public static function attrs($attrs, $charset) {         return new twig_markup(wxu::html_attrs($attrs), $charset);     } } 

bam! no more escaping.

see ptilz example implementation of html_attrs.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -