N
N
Night3862021-10-21 01:43:07
PHP
Night386, 2021-10-21 01:43:07

How to output variable in tpl twig template?

there is a code

public function display()
    {
      if (isset($_POST['fav']))
      {
        $id = (int)$_POST['fav'];

        if ($id<=0) die;
        
        $chk = (bool)db::i()->selectCell("SELECT added from c_user_favorite where id_user = ".(int)$this->user['id']." and id_object = ".(int)$id.";");

        if ($chk) db::i()->query("DELETE from c_user_favorite where id_user = ".(int)$this->user['id']." and id_object = ".(int)$id.";");
        else db::i()->query("INSERT INTO c_user_favorite (id_user, id_object, added) VALUES (".(int)$this->user['id'].", ".(int)$id.", NOW());");
      }
      else
      {
        tpl::i()->load('auth');

        /* select rows */

        $favIds = db::i()->selectArr("SELECT id_object AS _V FROM c_user_favorite where id_user = ".(int)$this->user['id']);
  echo count($favIds);
        if (count($favIds))
        {
          route::need('fs');

          $orders = array
          (
            '__photoid' => 'ФОТО', 
            'id' => 'ID', 
            '__l_deal_type__name' => 'СДЕЛКА', 
            'address' => 'АДРЕС', 
            'name' => 'НАЗВАНИЕ', 
            '__l_object_type__name' => 'ТИП ОБЪЕКТА', 
            '__l_object_metro__name' => 'МЕТРО', 
            'area' => 'МЕТРАЖ', 
            '__price' => 'ЦЕНА', 
            '__c_user_favorite__added' => 'ДОБАВЛЕНО'
          );

          tpl::i()->set('orders', $orders);
          tpl::i()->set('rows', fs::getObjects("c_object.id IN (".implode(",", $favIds ).") and c_object.id>0"));
        }

        tpl::i()->display();
      }
    }


echo count($favIds) gives me the number 5 . How can I display it in a twig template? tpl

<div class="panel panel-default">
                <div class="panel-heading">Избранное</div>
                <table class="table table-striped table-hover">
                  <thead>
                    {% for k,v in orders %}
                      <th {% if in_array(k, ['area', '__price']) %}style="text-align:right"{% endif %}><nobr>
                        {% if order == k %}
                          {% if sort == 'asc' %}<a href="{{url}}?page={{page}}&order={{k}}&sort=desc">{{v}} <i class="glyphicon glyphicon-chevron-up"></i></a>
                          {% else %}<a href="{{url}}?page={{page}}&order={{k}}&sort=asc">{{v}} <i class="glyphicon glyphicon-chevron-down"></i></a>
                          {% endif %}
                        {% else %}
                          <a href="{{url}}?page={{page}}&order={{k}}&sort=desc">{{v}}</a>
                        {% endif %}	
                      </nobr></th>
                    {% endfor %}
                  </thead>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nokimaro, 2021-10-21
@Night386

$countFav = count($favIds);
tpl::i()->set('countFav', $countFav);

in a template
{{ countFav }}
or
{{ countFav|default(0) }}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question