0

im reading Find substring in the string in TWIG

and on a single item it works fine for me.

<!-- language: lang-html -->
<li {% if 'page' in app.request.get('_route')|lower %}class="active"{% endif %}>
    <a href="{{path('adminPage')}}">
        <i class="fa fa-file-text"></i> <span>{% trans %}pages_text{% endtrans %}</span> <small class="label pull-right bg-{% if nav_options.count_pages > 0 %}primary{% else %}red{% endif %}">{{ nav_options.count_pages }}</small>
    </a>
</li>

now I find myself needing it to happen differently.

how would I do to find if "items in this array" are contained in "this substring"

the example would be something like this

{% if ['str1','str2'] in/contained in app.request.get('_route')|lower %}class="active"{% endif %}

I tried this and does not work.

I also would rather avoid using the "or" operator, if its a requirement so be it, but if it can be avoided, the better.

I even went and attempted a split parameter.

{% if 'page,blog'|split(',') in app.request.get('_route')|lower %}active {% endif %}

no dice!

the code is meant to allow me to have "several possible routes" inside a collapsible menu item, and if any of the possible route names (i have several route names, blog, blogAdd, blogEdit, blogPost etc) contains "blog" (same with page, and many others) the "active" class should be printed.

Community
  • 1
  • 1
JLChafardet
  • 59
  • 1
  • 14

2 Answers2

2

Creating a custom twig function would be an easier solution to use

$function = new Twig_SimpleFunction('array_in_string', function ($haystacks, $needle) {
    foreach($haystacks as $haystack) if (stripos($haystack, $needle) !== false) return true;
    return false;
});

$twig = new Twig_Environment($loader);
$twig->addFunction($function);

Then you call this function in Twig with :

{% if array_in_string(['page','blog',], app.request.get('_route')) %}
   {# do sthing #}
{% endif %}
DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • this was the closest solution so far, tho it needed a bit of tweaking. – JLChafardet Apr 25 '16 at 13:45
  • `code` $function = new Twig_SimpleFunction('array_in_string', function ($haystacks, $needle) { foreach($haystacks as $haystack) { if (stripos(strtolower($haystack), strtolower($needle)) > 0) { return true; } else { return false; } } }); $twig->addFunction($function); – JLChafardet Apr 25 '16 at 13:46
  • Strange you did `strtolower` as [stripos](http://php.net/manual/en/function.stripos.php) is case insensitive – DarkBee Apr 25 '16 at 18:00
  • because the path is in camelCase and the string to be checked is lowercase, so strtolower will get me what i need. – JLChafardet Apr 26 '16 at 18:46
0

I don't think you ought to be accessing HTTP stuff - eg get variables - in your view. That's kinda muddying yer MVC a bit.

I'd resolve all that in your controller, and just pass the result to the view (untested, obviously, so treat it as pseudocode):

// controller
$thingIsActive =  in_array(strtolower($request->get('_route')), ['str1','str2']);
return $app['twig']->render('whatever.twig', ['thingIsActive' => $thingIsActive]);

{# whatever.twig #}
<li{% if thingIsActive %} class="active"{% endif %}>
Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
  • Even though I agree here with you, I'm still in the process of figuring out how this should work, but for now the twig function seems to do the trick – JLChafardet Apr 25 '16 at 13:47