Str_Replace in Twig

Is there a way I can replace part of a string in twig?

You can extend twig with any PHP logic you want, e.g.

use Slim\Views\Twig;
use Slim\Views\TwigExtension;

$container['view'] = function ($container){
$twig = new Twig(__DIR__.'/../resources/views');

$twig->addFilter(new Twig_SimpleFilter('obfuscate', function($value, $char = '*', $visible = 4) {
if ($visible % 2 != 0) $visible++;
if (strlen($value) <= $visible) return str_repeat($char, strlen($value));
return substr($value, 0, floor($visible) / 2).str_repeat($char, strlen($value) - $visible).substr($value, -1 * (floor($visible) / 2));
}),

return $twig;
};

{{ 'john.doe@gmail.com' | obfuscate }} {# output: jo**************om  #}
{{ 'john.doe@gmail.com' | obfuscate('-', 10) }} {# output: john.--------l.com #}
{{ '123456' | obfuscate }} {# output: 12**56 #}
{{ '123' | obfuscate }} {# output: *** #}
{{ '1' | obfuscate }} {# output: * #}
{{ '+32497123456' | obfuscate }} {# output: +3********56 #}

String replace in Twig

This is a duplicate from this : if variable twig on symfony

Remember to check deeply on the forum before adding a question :)

Correct code would be :

{% set foto = 'photo_gallery/' ~ image.id ~ '/' %}
{% set thumbs = 'photo_gallery/thumbs/' ~ image.id ~ '/' %}

<img src="{{ pageTemplate.pageHeader.mainUrl }}{{ image.image |replace({foto: thumbs}) }}">

In twig, {% %} represent and execution, meaning what's inside will do something. When you are in those tags, you don't need to use {{ }} because twig will be able to interpret your variable names as long as they are not quoted.

More examples and details here : https://twig.symfony.com/doc/2.x/templates.html

How to use variables in Twig filter 'replace'

Either you pass the whole array, or you loop the replaces.

But when looping the replaces you need to wrap key and value in parentheses to force interpolation of those

{% set replaces = {
'{site}' : '{stackoverflow}',
'{date}' : "NOW"|date('d-m-Y'),
} %}

{% set haystack = '{site} foobar {site} {date} bar' %}

{{ haystack | replace(replaces) }}

{% set output = haystack %}
{% for key, value in replaces %}
{% set output = output|replace({(key) : (value),}) %}
{% endfor %}
{{ output }}

fiddle

Replace a string with array values in Twig

The arguments of your filter call is slightly incorrect, it should be something like this:

{% if Item.PictureName ends with '.jpg' %}
<img class="border rounded" src="http://5.12.82.223/ftp/images/{{ brand_number }}/{{ Item.PictureName|replace({'.jpg': '.JPG'}) }}">
{% endif %}

As described in the replace filter docs:

replace¶

The replace filter formats a given string by replacing the
placeholders (placeholders are free-form):

{{ "I like %this% and %that%."|replace({'%this%': foo, '%that%':
"bar"}) }}

{# outputs I like foo and bar if the foo parameter equals to the
foo string. #}

{# using % as a delimiter is purely conventional and optional #}

{{ "I like this and --that--."|replace({'this': foo, '--that--':
"bar"}) }}

{# outputs I like foo and bar #}

Arguments¶

from: The placeholder values


Related Topics



Leave a reply



Submit