default userpic

Modifiers, whitespace: strip and regex_replace

Vote 0 Votes

Just curious: are there cases in which these would return different results, or is the strip modifier a shortcut for the regex version?

<mt:Var name="test" strip="">

<mt:Var name="test" regex_replace="/\s+/g","">

Reported on Movable Type 4.2

3 Replies

| Add a Reply
  • Hi Alex -

    As you have it, the strip modifier won't do anything, because you have to provide something in order for it to work. Right now, you aren't passing anything in, so it will not execute at all. Typically you pass a value in and that value is then replaced.

    Meanwhile the regex_replace modifier is used correctly - you are replacing all instances of multiple spaces with nothing.

    You might want to look over the modifier documentation: http://www.movabletype.org/documentation/appendices/modifiers/

  • Hi Chad,

    I was aiming in another direction with my question. To clarify, the results of the code examples are identical, whether you use strip or regex_replace. In both cases the modifiers modify "test" - whatever "test" holds. For example, it could be this:

    <mt:SubCategories category="News">
    <mt:SetVarBlock name="test" function="push"><mt:CategoryLabel /></mt:SetVarBlock>
    </mt:SubCategories>

    Since the results are the same, I was wondering why Six Apart created the strip modifier. Hence, my question. Do you still think the strip modifier has nothing to work on in the example?

    Btw, I was looking for a way to strip whitespace that would leave any HTML tags intact, stripping only line breaks and other whitespace between text and HTML tags. That would involve a more complex regex with some sort of Lookaround I guess, but I can live without that for now.

  • Hi Alex -

    In short, I'd say that the reason for the "strip" attribute is that it's a shortcut - a simpler way to get at the regex_replace function for most people, though it won't be as powerful. If you look at the code, you'll see that the routine used looks a lot like the regex_replace function.

    s/\s+/$val/g
    

    But you don't have the trouble of creating a regex.

    Still, what I said earlier holds - to some extent. The strip function only works if you assign a value - it won't work to replace with null (as you are doing). It's designed to replace whitespace with "something". The first thing it does is check for "something". If "something" is empty, then it sets "something" ($val) to a single space.

    So in your example, the value of test (CategoryLabel) will be replaced with a single space. Meanwhile, using regex_replace should work the way you want - to replace CategoryLabel with nothing at all (or a string of whitespace as in your first example).

    That means there is a difference - albeit a small one.

Add a Reply

If you need to share template code, replace all the "<" signs with "&lt;" or use this utility.

Forum Groups

1773 6162

Last Topic: Excluding categories from blog by kholechek on Feb 9, 2012

86 302

Last Topic: website entries by masoud on Oct 26, 2011

1429 5077

Last Topic: What apocalypse hit this community in the middle of 2011? by 75th on Feb 10, 2012

695 2910

Last Topic: Insert Image / File Fails by Russ Miller on Feb 10, 2012

84 291

Last Topic: How to have some other characters in entry basename automatically written by Afshin Haghighatnia on Dec 22, 2011

173 737

Last Topic: About the MT version stated in HTML source by Alex E. Schneider on Feb 7, 2012

190 567

Last Topic: Analytics Reporting by michael webster on Feb 5, 2012

48 210

Last Topic: An idea and also a request by Afshin Haghighatnia on Jun 29, 2011

64 246

Last Topic: jQuery in MT 5.1 still at 1.4 - why? by perlmonkey on May 25, 2011

code.sixapart.com

137 478

Last Topic: Getting a thumbnail with xpath by Peter on Mar 13, 2011

222 720

Last Topic: Custom Field for Asset Not Appearing by android on Feb 9, 2012