3

I've already a solution, but just for JavaScript. Unfortunately while-loops do not exist in Twig.

My Twig-target in JavaScript:

var x = 10; // this is an unknown number
var result = x;
while (100 % result !== 0) {
   result++;
}
console.log(result);

Any ideas how I do this in Twig?

What's my target: (not important if you already understood)

I want to get the first number after my unknown number, that satisfy the following condition:

100 divided by (the first number) equals a whole number as result.

EDIT: I have no access to PHP nor Twig-core.

ReeCube
  • 2,545
  • 17
  • 23
  • Twig haven't conditional logic loop. you can make your custom extension with the custom logic. I don't think you need something of [this](http://www.craftitonline.com/2011/10/closured-iterator-the-secret-while-twig-tag/) – Matteo Feb 19 '15 at 11:22

2 Answers2

3

You can make a Twig extension like:

namespace Acme\DemoBundle\Twig\Extension;

class NumberExtension extends \Twig_Extension
{

    public function nextNumber($x)
    {
        $result = $x;
        while (100 % $result !== 0) {
            $result++;
        }
        return $result;
    }

    public function getFunctions()
    {
        return array(
            'nextNumber' => new \Twig_Function_Method($this, 'nextNumber')
        );
    }

    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    public function getName()
    {
        return 'demo_number';
    }
}

And define it in the service.xml of the bundle:

<service id="twig.extension.acme.demo" class="Acme\DemoBundle\Twig\Extension\NumberExtension">
    <tag name="twig.extension" />
</service>

Then use it in the template:

{{ nextNumber(10) }}

UPDATE

A (not so great) approach but that possibly satisfy your needed is to do something like this:

{% set number = 10 %}
{% set max = number + 10000 %} {# if you can define a limit #}
{% set result = -1 %}
    {% for i in number..max %}
        {% if 100 % i == 0 and result < 0 %} {# the exit condition #}
            {% set result = i %}
        {% endif %}
    {% endfor %}

<h1>{{ result }}</h1>

Hope this helps

msa
  • 702
  • 7
  • 14
Matteo
  • 37,680
  • 11
  • 100
  • 115
  • Good answer, but the problem is, that I've no access to the twig initialisation, so I can't add custom extensions... – ReeCube Feb 19 '15 at 11:33
  • ah sorry, i post an implementation based on the `symfony2` framework :) . In what environment are you? @ReeCube – Matteo Feb 19 '15 at 11:34
  • You can see how to define a custom `TWIG function` [here](http://twig.sensiolabs.org/doc/advanced.html#functions) depends of which version of the library are you using. Let me know how i can help you. – Matteo Feb 19 '15 at 11:41
  • we have our own CMS in our enterprise but the problem is, that Twig is located on the core and I have no access to the core nor PHP, only Twig... but anywas thanks for your help :) – ReeCube Feb 19 '15 at 11:42
  • Hi @ReeCube i update my solution, check if this satify your needed! – Matteo Feb 19 '15 at 12:13
  • Saw it, looks nice. Yes I have a max number, it's 100. The problem is it does not exit the for-loop so I get allways the max number... – ReeCube Feb 19 '15 at 12:19
  • 1
    add {% set result = -1 %} and {% if 100 % i == 0 and result < 0 %} then it works as i wanted it to work, thanks a lot :D – ReeCube Feb 19 '15 at 12:20
0

In my case - I had to output an object with similar subobjects - including a templete with predefined values and setting up a normal if-condition worked.

See http://twig.sensiolabs.org/doc/tags/include.html for more infomration.

nikma
  • 3
  • 4
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Raju May 30 '16 at 23:15