Search and Replace

Cupara

Well-known member
I'm sure I used it before but can anyone recall the line of code to execute within a mod to replace a specific line within a template with a new line when installing the mod?

Similar to vB's fine <xen:comment> and replace with <xen:comment>This goes here...
 
I'm sure I used it before but can anyone recall the line of code to execute within a mod to replace a specific line within a template with a new line when installing the mod?

Similar to vB's fine <xen:comment> and replace with <xen:comment>This goes here...

No, there's no helper to search&replace some text in templates. If you want to replace the XenForo Raw Code in template, use TMS, otherwise you can modify parsed template code with two of the three template listeners: one at the hook level, the other one at the template level once it has been parsed.

In any cases, even with TMS, you can use direct text replacements (str_replace) or regex replacements (preg_replace).
 
I prefer not to use TMS.

What is the process for search and replace at the template level after it has been parsed?
 
No, there's no helper to search&replace some text in templates. If you want to replace the XenForo Raw Code in template, use TMS, otherwise you can modify parsed template code with two of the three template listeners: one at the hook level, the other one at the template level once it has been parsed.

In any cases, even with TMS, you can use direct text replacements (str_replace) or regex replacements (preg_replace).
I was looking for the same thing. I'm surprised this does not exist..
 
I prefer not to use TMS.

What is the process for search and replace at the template level after it has been parsed?

I told you, create a new listener "template_post_render" to target a whole template or "template_hook" to target the content of a hook. In both cases, the content will be already parsed. Then to modify the content just modify on the fly the variable $content (or $contents, depending on the callback signature)
Basic example:
PHP:
$content = str_replace('this was before', 'this is now', $content);

Now don't forget, these change will be made when the page is loading, where as with TMS these changes will already be cached. Both methods are useful, all depend of what you want to do.
When you can use TMS, just use it. It will save up some server resources.
 
Back
Top Bottom