Wondering if it's possible to use regex for the ^contains keyword modifier. I know it can be used for ^replace, but I use ^contains a lot more as it provides me with (very basic) if/else logic.
Manual don't seem to indicate it is do-able, only for ^replace.
I can get around it (sometimes) with multiple keywords all using contains, but only if my 'else' logic is to display nothing for all cases.
Matrix v4.12
If not possible, is it planned for future release? Any others who would think this would be nice feature?
Thanks Bart. preg_replace looks pretty nice, but as you say is in a later version.
The specific functionality I was after was being able show/hide content based on which section of the site the user is in based on the top-level page (while avoiding design customisations). And then make it re-usable so it can be nested in multiple places within the site, update once, update everywhere, etc. I was doing this by inspecting the url. But a nuance of our system is we have replicated master/slave server setup, and the master url's don't match the slave url's 1:1, not even close.
It works, but I didn't like it, specifically because 'do_stuff_here' is the same for both lines, but I've got to define it twice.
Then repeat that for each item you want to display/hide, in these cases lists of links, so a couple hundred lines of code (so far).
If I could have used regex in the ^contains modifier I could have captured both those conditions in the one line.
Also, I know could have been achieved a number of other ways (e.g. design customisations, pure css, keywords with HTML comment bracing, javascript) but each of these options was ruled out for one reason or another. Above approach is working for now, albeit more verbose than I would like.
Looks like I can look forward to this functionality in a later version.
What content are you printing conditionally? Hardcoded HTML in the parse file or can you use nesting for it? The good thing in 5.0 is that you can use the conditional content feature on standard pages, so you can have a global complex condition setup using regex, then just %begin %else %end keywords to print everything.
To make that keyword you are using for now easier though, maybe just use the replace modifier first and then the contains modifier? That way you can do it all in 1 line, something like:
Aha, that's an excellent idea (daisy-chaining replace before contains, I mean). I will try that out today and let you know how it goes, but that should definitely capture it nicely.
Looking forward to some of the nice features in v5, also.
This basically strips back the 'nuisance' part back to the barebones, and then the contains figures out the rest.
This method of stripping out the nuisance part with replace first is something I'm going to be able to re-use again and again. Above example is relatively simple, but with more and more complex regex in the replace you could really do so much more.
The first screws up completely because the parser can't handle the nested %% pair around the keyword %ds__title%. Surely there is a way to do this?
Even if it can be made to work it is a very cumbersome method. I can't for the life me see why Matrix does not have a general "if then else " so that you could write say
% if (%ds__event-type^contains:Conference%) %
<p> some output </p>
%else%
<p>some other output</p>
%endif%
Very flexible and simple and particulary useful for asset listings of data source records.