带有typeahead.js的Symfony2:如何将%QUERY传递给path参数

时间:2014-02-18 12:36:40

标签: javascript symfony typeahead.js

我想在我的symfony2项目中使用typeahead.js。因此我有以下javascript:

    $(document).ready(function(){

        var players = new Bloodhound({
          datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.value); },
          queryTokenizer: Bloodhound.tokenizers.whitespace,
          remote: "{{ path('_api_player_search', {searchterm: '%QUERY', limit: 5}) }}",
          prefetch: ''
        });

        players.initialize();

        $('#searchfield').typeahead(null, {
          displayKey: 'firstname',
          source: players.ttAdapter(),
          templates: {
            suggestion: Handlebars.compile(
              '{% verbatim %}<p><strong>{{firstname}}</strong> – {{lastname}}</p>{% endverbatim %}'
            )
          }
        });
    });

如您所见,我想将twig path帮助程序创建的URL传递给remote:配置的Bloodhound()属性。 (后来我想为此目的使用FOSJsRoutingBundle。)

现在的问题是,Bloodhound()不会替换我的twig表达式中的%QUERY占位符。有没有办法实现这个目标?

非常感谢您的帮助! :)

1 个答案:

答案 0 :(得分:1)

好的,我找到了解决方案:

根据@Peter Bailey answer对此question我创建了一个自定义树枝扩展名,以便能够将{url_decode过滤器添加到我的{{ path() }}标记{{ path() |url_decode }}

这会阻止%QUERY转换为%25QUERY,然后可以通过javascript在呈现的网址中替换它。

此外,我还添加了|raw过滤器,以避免编码的&符号。最后我的标签看起来像这样:{{ path() |url_decode|raw }}