I
I
infastin2015-03-27 12:49:56
PHP
infastin, 2015-03-27 12:49:56

How to duplicate a file in a PHP loop and output to the main Twig template?

There is a template:

<div class="col-md-4">
  <div class="panel panel-default">
    <div class="panel-heading">
      <center>
        {{ repositories.name }}
      </center>
    </div>
    <div class="panel-body">
      <center>
        <p id="version">Версия: 1.0.10 Alpha</p>
        <div id="buttons">
          <a href="#" class="btn btn-success">Скачать</a>
          <a href="#" class="btn btn-info">Подробнее</a>
        </div>
      </center>
    </div>
  </div>
</div>

This template must be duplicated in the PHP loop and output to the main.layout template, the template itself:
{% extends "base.layout" %}

{% block title %} Главная {% endblock title %}

{% block content %}
<center>
  <h1>Репозитории</h1>
</center>
<hr />
<!-- Repositories -->
{{ repos }}
<!-- end repositories -->
{% endblock content %}

In {{ repos }} you need to insert the duplicated template.
That should be something like this:
{% extends "base.layout" %}

{% block title %} Главная {% endblock title %}

{% block content %}
<center>
  <h1>Репозитории</h1>
</center>
<hr />
<!-- Repositories -->
<div class="col-md-4">
  <div class="panel panel-default">
    <div class="panel-heading">
      <center>
        Title
      </center>
    </div>
    <div class="panel-body">
      <center>
        <p id="version">Версия: 1.0.10 Alpha</p>
        <div id="buttons">
          <a href="#" class="btn btn-success">Скачать</a>
          <a href="#" class="btn btn-info">Подробнее</a>
        </div>
      </center>
    </div>
  </div>
</div>
<div class="col-md-4">
  <div class="panel panel-default">
    <div class="panel-heading">
      <center>
        Title
      </center>
    </div>
    <div class="panel-body">
      <center>
        <p id="version">Версия: 1.0.10 Alpha</p>
        <div id="buttons">
          <a href="#" class="btn btn-success">Скачать</a>
          <a href="#" class="btn btn-info">Подробнее</a>
        </div>
      </center>
    </div>
  </div>
</div>
<!-- end repositories -->
{% endblock content %}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
skynetdev, 2015-03-28
@infastin

Try it with the filter
twig.sensiolabs.org/doc/advanced.html
it should look something like this

....
       // регим фильтр в твиге
        $filter = new \Twig_SimpleFilter('dublicate', function($repo){
            return str_repeat($repo,2);  // здесь для дублирования можешь использовать и цикл если тебе сильно надо
        }, array('is_safe' => array('html');
        $this->twig->addFilter($filter);
.....

in the template itself
{% extends "base.layout" %}

{% block title %} Главная {% endblock title %}

{% block content %}
<center>
  <h1>Репозитории</h1>
</center>
<hr />
<!-- Repositories -->
{{ repos|dublicate }}
<!-- end repositories -->
{% endblock content %}

Then try it yourself.
I hope everything works out for you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question