0

I want to send a part of HTML as a variable inside the array params in $this->render() function.

I use this variable in my view, {{ other_product | raw }} but it doesn't format the TWIG syntax as well and get the correct path.

 public function productsAction(){
    $em = $this->getDoctrine()->getManager();
    $produits =    $em->getRepository('AppBundle:Products')->findBy(array('disponibility' => 1));

    $other_product='<a href="{{ path(similar_product) }}" target="_blank">Other product</a>';

    return $this->render('AppBundle:Default:products/layout/products.html.twig', array(
        'products' => $products,
        'other_product' => $other_product)
    );
}

This is the result <a href="{{ path(similar_product) }}" target="_blank">Other product</a> How to format or filter this variable ?

WorldTICR
  • 11
  • 2
  • Possible duplicate of [Patternlab / Twig Variable Interpolation doesn't work with string from json](https://stackoverflow.com/questions/50955886/patternlab-twig-variable-interpolation-doesnt-work-with-string-from-json) – DarkBee Jan 16 '19 at 08:33
  • Thank you @DarkBee for your answer but I have a new idea after seeing this https://stackoverflow.com/questions/31081910/what-to-use-instead-of-twig-loader-string – WorldTICR Jan 16 '19 at 09:32
  • Should be a better solution seeing that `Twig_Loader_String` is deprecated now – DarkBee Jan 16 '19 at 09:36
  • $templates = array('part_html_twig' => '{{ text_link }}'); $env = new \Twig_Environment(new \Twig_Loader_Array($templates)); – WorldTICR Jan 16 '19 at 09:36
  • I do that in the `render()` function `return $this->render('AppBundle:Default:products/layout/products.html.twig', array( 'products' => $products, 'part_html' => $env->render('part_html_twig', array( 'route_name' => 'similar_product', 'text_link' => 'Other product' ) )) );` then `{{ part_html |raw }} ` to the view and it's work !!! – WorldTICR Jan 16 '19 at 09:49
  • Make it an answer and self-accept it then :) – DarkBee Jan 16 '19 at 10:02

0 Answers0