Exploring new <Format> functions

I wanted to create this to ask some questions about the new Format functions released in 3.2.8.

The first one I want to ask about is how to use L&L to access array data. Now that we have Format regex that is outputting an array. Based on the docs, it looks like we can do this with <Format list index=0>, <Format list index=1> etc which is great.

This works, but I’m also getting a PHP notice of "Only variables should be passed by reference in /wp-content/plugins/tangible-loops-and-logic/vendor/tangible/template-system/template/format/list.php on line 22"

Another thing I wanted to see is if capture groups work. I did some testing, and from what I can tell, they do not… Is this something that can be added that could go in the with="$1"

An example could be something like <Format replace="/(\d{3})(\d{3})(\d{4})/" with="$1-$2-$3">1234567890</Format> would output 123-456-7890

@eliot

Line 22 is:

if (is_array( $content )) {
    return array_shift( array_slice($content, $index, 1) );
  }

I believe there is no need to pass the result by reference? If so, the warning can be fixed by assigning the result to a variable and passing that variable instead:

if (is_array( $content )) {
  $slicedArray = array_slice($content, $index, 1);
  return array_shift($slicedArray);
}

I would suggest this approach to using the match_pattern output:

<Set output><Format match_pattern="/\d+/">Test 123 and 456</Format></Set>
<ul>
<Loop list="{Get output}">
  <li><Field /></li>
</Loop>
</ul>

<Loop list=""> accepts lists set with the List tag and JSON. It’s (not fully) documented here

1 Like

Good to know that can pull them out if you want all in a multiple item list scenario.

(as apposed to the method for just a one item list or needing a specific item <Format list index=0>)

Hi Zack, thanks for starting this discussion thread about the new format functions.

About the PHP notice: indeed, that’s due to the peculiarity of PHP array functions, that some of them are not able to be chained together. Exactly, as you suggested, there needs to be a temporary variable to pass to array_shift. I’ll apply your fix and release in the next version soon. I’m surprised it still works with the notice.

About capture groups: great idea, I’ll look into adding support for this.

<Format replace_pattern="/(\d{3})(\d{3})(\d{4})/" with="$1-$2-$3">1234567890</Format>
1 Like

Awesome! Thanks @eliot

These are going to be some powerful tools, especially if we have capture groups too!

Hey @eliot

Just tested the new capture group feature. Works like a charm :heart_eyes:

@eliot

I see you can do replace on a <Field> like:
<Field phone_number replace=" " with="-" />

Are there plans to also include replace_pattern or match_pattern on <Field> as well?
So instead of:

<Format replace_pattern="/(\d{3})(\d{3})(\d{4})/" with="$1-$2-$3">
<Field phone_number />
</Format>

you could have:
<Field phone_number replace_pattern="/(\d{3})(\d{3})(\d{4})/" with="$1-$2-$3" />