Symfony / Twig变量“ replaceString”不存在

时间:2018-06-30 09:16:52

标签: php symfony twig

我不明白为什么他找不到我的变量,然后定义得很好。

我的错误:

变量“ replaceString”不存在。

in templates/scraping/index.html.twig (line 5)

{% extends 'base.html.twig' %}
{% block bodyIndex %}
    <ul class="list-group list-group-flush">        
         {% for rS in replaceString %}            
             <li class="list-group-item">{{ rS }}</li>        
         {% endfor %}
   </ul>
{% endblock %}

控制器:

class ScrapingController extends Controller
{
    /**
     * @Route("/scraping", name="scraping")
     */
    public function getMovie()
    {

        $client = new Client();

        $crawler = $client->request('GET', 'http://www.torrents9.cc/torrents_films.html,trie-seeds-d');

        // On filtre sur les éléments pour les récuperer
        $crawler->filter('tbody > tr > td > a')->each(function ($node) {

            // On remplace les strings en trop par une chaine vide avec str_replace
            $emptyString = array("FRENCH", "TS", "DVDRIP", "WEBRIP", "TRUEFRENCH", "TRUE", "PROPER");
            $replaceString = str_replace($emptyString, "",$node->text()."\n");

            return $this->render('scraping/index.html.twig', array(
                'replaceString' => $replaceString
            ));
        });

        return $this->render('scraping/index.html.twig', [
            'crawlers' => $crawler
        ]);

    }

}

嫩枝:

{% extends 'base.html.twig' %}

{% block bodyIndex %}
    <ul class="list-group list-group-flush">
        {% for rS in replaceString %}
            <li class="list-group-item">{{ rS }}</li>
        {% endfor %}
    </ul>
{% endblock %}

我想在树枝上张贴从其他网站检索的电影列表

谢谢。

1 个答案:

答案 0 :(得分:1)

将控制器更改为这样。

class ScrapingController extends Controller
{
    /**
     * @Route("/scraping", name="scraping")
     */
    public function getMovie()
    {

        $client = new Client();
        $replaceString = array();

        $crawler = $client->request('GET', 'http://www.torrents9.cc/torrents_films.html,trie-seeds-d');

        // On filtre sur les éléments pour les récuperer
        $crawler->filter('tbody > tr > td > a')->each(function ($node) {

            // On remplace les strings en trop par une chaine vide avec str_replace
            $emptyString = array("FRENCH", "TS", "DVDRIP", "WEBRIP", "TRUEFRENCH", "TRUE", "PROPER");
            $replaceString[] = str_replace($emptyString, "",$node->text()."\n");

        });

        return $this->render('scraping/index.html.twig', [
            'crawlers' => $crawler,
            'replaceString' => $replaceString
        ]);

    }

}