包含动态自动建议jig代码在

时间:2018-06-04 07:02:51

标签: symfony-3.4

我想将学生代码的自动建议列表添加到输入框

public function autoCodeSuggest()
{
    $em = $this->getDoctrine()->getManager();
    $query = $em->createQuery("SELECT s.studentLogin FROM AppBundle:StudentLogin s ORDER BY s.studentLogin");
    $code = $query->getArrayResult();
    $strJavascript = '';
    if (!empty($code)){
      $strJavascript = '
      var ArrayCode = new Array(';
      for ($i=0; $i<count($code); $i++){
          $strJavascript .=  '"'.$code[$i]['studentLogin'].'",';
      } // for ($i=0; $i<count($code); $i++)
      $n = strlen($strJavascript)-1;
      $strJavascript = substr_replace($strJavascript,'',$n); // remove last ,
      $strJavascript .= ');';
    } // if (!empty($code))
    return $strJavascript;
} // end function

在我的控制器中

public function studentSearchAction()
{
    $LoginJS = $this->get('utilities_student_tools')->autoCodeSuggest();
    return $this->render('student/student_search.html.twig', array(
        'LoginJS' => $LoginJS,
    ));
}

student_search.html.twig包含

{% block body %}
<script language="javascript" type="text/javascript">
{{ LoginJS }}
</script>
{{ include('student_code.html.twig') }}
{% endblock %}

它不起作用,因为当我查看我的页面的源代码时,我有

<script language="javascript" type="text/javascript">
      var ArrayCode = new Array(&quot;AA0951&quot;,&quot;AA1825&quot;,&quot;AA2802&quot;,&quot;AA2886&quot;,&quot;AA3418&quot;,.....
</script>

当我向javascript代码生成器添加\时

$strJavascript .=  '\"'.$code[$i]['studentLogin'].'\",';

输出变为

var ArrayCode = new Array(\&quot;AA0951\&quot;,\&quot;AA1825\&quot;,\&quot;AA2802\&quot;,\&quot;AA2886\&quot;

如果输出类似

,则有效
var ArrayCode = new Array("AA0951","AA1825","AA2802","AA2886",

the " is converted to &quot; . 

如何避免树枝转换?

1 个答案:

答案 0 :(得分:0)

我找到了答案。

{{ LoginJS|raw }}