user-pic

how to use variables in regex_replace

Vote 0 Votes

Sorry for repeating, but I really really need to know:

How do you use variables within the regex_replace attribute??

The following works:


<mt:var name="body_content"
regex_replace="/<span class="mt-enclosure .+?><img
alt="(.+?)" .+?</span>/g","$new_image_block"
setvar="body_content" />

but I need to put the current asset's label* into that search text, like the following but I'd prefer something that actually works.


<mt:var name="body_content"
regex_replace="/<span class="mt-enclosure .+?><img
alt="$my_image_label" .+?</span>/g","$new_image_block"
setvar="body_content" />

That doesn't work, so can you please tell me what would? I haven't found a way of approaching this that doesn't involve me needing a variable in that search text.

* Yes, this is all within the context. I'm looking for the span that contains the current asset's label, and replacing that whole span with a different block of HTML.

Help, please!!

Reported on Movable Type 4.2

12 Replies

| Add a Reply
  • It doesn't appear that you can use variables in the regex_replace modifier (at least through my own tests as well as yours). You can use them in the regular 'ol replace modifier. Possibly if you set up more easily searchable conditions you could just use the replace modifier instead. It seems like it's touchy ground with regex_replace, since we know how much regex likes the $ sign, and how much MT likes it as well.

    I could be wrong… but I could be right.

  • Thanks, Pedro. It's a good suggestion, but I'd need to know exactly what the span will be like for a given asset, and I don't -- in particular, I don't know what the width & height of the thumbnail will be.

    If I could just find the spans and put them into a variable (or a variable array)... but I haven't figured that out yet, either. :-P

  • Well, if anything you could use some PHP or other alternative, unless the code you're trying to insert uses some MT tags or something like that.

  • Uh, crud, how do I do that? Nevermind, I'll look that up later.

    mt variables worked for you within replace? It's failing for me. What am I doing wrong, please?

    <mt:var name="body_content" replace="$my_asset_label","FNORDDRONF$my_asset_id" setvar="body_content" />
    
    

  • Sorry, Pedro, I shoulda said in what way it wasn't working: all of the asset labels are being replaced by "FNORDDRONF$my_asset_id" without that $my_asset_id being replaced by the actual id.

  • In my experimenting, you can't do that. You have to only have just the variable in there

    <mt:var name="body_content" replace="$my_asset_label","$my_asset_id" setvar="body_content" />

    If you wanted to modify that, you could set up a variable block to combine "FNORDDRONF" and the value of $my_asset_id. It would look something like this:

    <mt:setvarblock name="new_image_text">"FNORDDRONF"<$mt:var name="$my_asset_id" $></mt:setvarblock>
  • Oops, I had a typo in there. You don't need the quotes around "FNORDDRONF" in the variable block (that's what I get for copying/pasting). Variable blocks are smart enough to process everything in them like MT normally would and put all the returning values into the variable block - so you can put all sorts of code as well as plain text, and it'll just plow right through it.

  • You might want to run this scenario by the MTOS developer mailing list to see if this sort of scenario is supported by the template engine.

  • ***If you do post it on the MTOS mailing list, and they say it is possible, it would be nice if you could take a few minutes to document what you tried to accomplish and how you did it so that others could benefit from more documentation.

  • Mike T, I'm not familiar with the MTOS developer mailing list, sorry.

  • Thanks, Mike. But now that I re-read the thread, I'm wondering -- which scenario did you mean? I'm going to ask there about the scenario more fully fleshed out in my most recent question, but the scenarios here are, I think, confirmed:

    In regex_replace, you can only use a variable as the replace pattern, not as the search pattern.

    In replace, you can use a variable as either the replace or search texts, or both, but you have to just use a variable; "fubar$my_var" doesn't work. (Thanks, Pedro!!)

    hope that helps someone.

    Mike & Pedro & others, thanks for helping so much during this latest development rush/panic.

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