MarcusBarnes / mik

The Move to Islandora Kit is an extensible PHP command-line tool for converting source content and metadata into packages suitable for importing into Islandora (or other digital repository and preservations systems).
GNU General Public License v3.0
34 stars 11 forks source link

regex_replace filter not available? #486

Closed bondjimbond closed 5 years ago

bondjimbond commented 5 years ago

[2018-08-29 18:17:47] ErrorException.ERROR: ErrorException {"message":"problem with metadataparser","record_key":669,"details":"[object] (Twig_Error_Syntax(code: 0): Unknown \"regex_replace\" filter. at /Users/brandon/sfuvault/mik/manipulations/athabasca_manipulations_nicknames/coordinates_herbarium.xml:4)"} []

My code:

{% if latitu %}
<subject>
  <cartographics>
    <coordinates>{{ latitu|regex_replace(['/,/','/$/'],['/º/','/N/']) }}, {{ longit }}</coordinates>
  </cartographics>
</subject>
{% endif %}

Is there no regex_replace filter available, or did I just do a bad job writing it?

mjordan commented 5 years ago

MIK uses Twig version 1 - is regex_replace new in version 2? All Twig version 1 filters/functions/etc should work.

bondjimbond commented 5 years ago

As far as I know it's in both... not seeing anything in the changelog: https://github.com/twigphp/Twig/blob/2.x/CHANGELOG

Soooo... no idea why it's not recognizing my filter then?

mjordan commented 5 years ago

Let me try it in a Templated parser to see if it works. Late today.

mjordan commented 5 years ago

It's not listed at https://twig.symfony.com/doc/1.x/ or at https://twig.symfony.com/doc/2.x/ - where is it documented?

bondjimbond commented 5 years ago

https://learn.getgrav.org/themes/twig-filters-functions

https://www.branchcms.com/learn/docs/developer/twig/filters/item/regex-replace

And other places

mjordan commented 5 years ago

I think those two platforms provide regex_replace as a Twig extension. It doesn't appear to come as part of Twig itself.

MIK does provide an example of adding a custom extension, twigTruncate, at https://github.com/MarcusBarnes/mik/blob/master/src/utilities/MikTwigExtension.php.

mjordan commented 5 years ago

... and I failed to add, we could implement our own regex_replace there if we wanted to.

bondjimbond commented 5 years ago

No worries. Looks like I was able to get what I need out of a couple of other functions plus the "replace" function.

mjordan commented 5 years ago

Cool. That said, if we want to extend Twig, we can do that.