Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Wellwisher on March 30, 2016, 01:34:39 PM

Title: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Wellwisher on March 30, 2016, 01:34:39 PM
I have "hooked" in all my functions (thanks to @Margarett's hooks tutorial & advise from @Jblaze, @Nend & @Shambles - yay!).
Feels AWESOME (https://www.youtube.com/watch?v=I_izvAbhExY).  8) I am now left with *one* line of code which requires a theme edit, and I am lost - because it involves php.

This following example snippet is from "Install.XML" of a mod which packman uses to execute add/ replace operations in theme files.

My question is: How do I make this type of edit to a theme file from my Mod.php (is it possible?) without resorting to editing them:

<file name="$themedir/Display.template.php">
<operation>
<search position="before"><![CDATA[// Show the post itself, finally!]]></search>
<add><![CDATA[
// Enter my code
                           ]]>
</add>
</operation>
</file>
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: JBlaze on March 30, 2016, 02:28:53 PM
I'm not sure I understand what you're asking entirely. You want to make a template edit after the mod has been installed? If so, why? It's much, much easier to make the edit when installing the mod itself.
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Wellwisher on March 30, 2016, 02:32:04 PM
I am failing to add code to display.template.php during the mod installation from inside my mod file.

Basically this hooking business is rather addictive and thus, I want this to happen from inside my mod file. Doesn't look like there is a hook to add code inside display.template.php?
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: JBlaze on March 30, 2016, 02:34:04 PM
What exactly is failing? Is it giving an error on install, or is it not adding the actual code to the template?
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Wellwisher on March 30, 2016, 02:36:32 PM
Quote from: JBlaze on March 30, 2016, 02:34:04 PM
Is it not adding the actual code to the template?

That one.^  ;D
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Illori on March 30, 2016, 02:40:22 PM
without the actual code you are trying to add and what is in your mod file, we can guess all day as to why it is failing and never have the exact right answer.
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Wellwisher on March 30, 2016, 03:10:10 PM
Quote from: Illori on March 30, 2016, 02:40:22 PM
without the actual code you are trying to add and what is in your mod file, we can guess all day as to why it is failing and never have the exact right answer.

Hello Illori,

I am asking in general in SMF. Say if I wanted to add a line of code to a $theme directory file. How can this be achieve from my mod file? Is this possible/ allowed?

Resorting to install.xml to make these edits happen, makes me feel like a dirty hobo. :-\
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Illori on March 30, 2016, 03:16:31 PM
many mods do this. check the mods on the mod site otherwise we cant tell you what may be going wrong.
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Wellwisher on March 30, 2016, 03:29:45 PM
Quote from: Illori on March 30, 2016, 03:16:31 PM
many mods do this. check the mods on the mod site otherwise we cant tell you what may be going wrong.

If it's not too much trouble can you refer me to a mod that does this (maybe your own)?
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: JBlaze on March 30, 2016, 03:33:13 PM
Quote from: Wellwisher on March 30, 2016, 03:10:10 PM
Resorting to install.xml to make these edits happen, makes me feel like a dirty hobo. :-\
That's how SMF modifications work... So unless you want to rewrite the package manager, use install.xml for your file edits.
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Wellwisher on March 30, 2016, 03:52:56 PM
Quote from: JBlaze on March 30, 2016, 03:33:13 PM
Quote from: Wellwisher on March 30, 2016, 03:10:10 PM
Resorting to install.xml to make these edits happen, makes me feel like a dirty hobo. :-\
That's how SMF modifications work... So unless you want to rewrite the package manager, use install.xml for your file edits.

My d00d, Illori said it's possible but he's hogging the trade secert or making me look for it like a needle in a hay-stack.  :P

Quote from: Illori on March 30, 2016, 03:16:31 PM
many mods do this. check the mods on the mod site otherwise we cant tell you what may be going wrong.
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Illori on March 30, 2016, 03:55:27 PM
yes many mods add edits to the install.xml file for theme edits... that is what you asked about.
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Wellwisher on March 30, 2016, 04:01:52 PM
Quote from: Illori on March 30, 2016, 03:55:27 PM
yes many mods add edits to the install.xml file for theme edits... that is what you asked about.

So this is not possible to do via my Mod.php file? That's my original question, be it a little confusing to understand.  ::)
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: JBlaze on March 30, 2016, 04:17:01 PM
Quote from: Wellwisher on March 30, 2016, 04:01:52 PM
So this is not possible to do via my Mod.php file?
Nope.
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Wellwisher on March 30, 2016, 04:23:00 PM
Quote from: JBlaze on March 30, 2016, 04:17:01 PM
Quote from: Wellwisher on March 30, 2016, 04:01:52 PM
So this is not possible to do via my Mod.php file?
Nope.

Peckers. :( I so wanted to kick install.xml to the curb.
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Wellwisher on April 03, 2016, 05:46:52 PM
Quote from: JBlaze on March 30, 2016, 04:17:01 PM
Quote from: Wellwisher on March 30, 2016, 04:01:52 PM
So this is not possible to do via my Mod.php file?
Nope.

Guys can a work-around be achieved using php namespaces?
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: live627 on April 03, 2016, 06:23:56 PM
Nope.
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Matthew K. on April 03, 2016, 06:43:36 PM
Quote from: Wellwisher on April 03, 2016, 05:46:52 PM
Quote from: JBlaze on March 30, 2016, 04:17:01 PM
Quote from: Wellwisher on March 30, 2016, 04:01:52 PM
So this is not possible to do via my Mod.php file?
Nope.

Guys can a work-around be achieved using php namespaces?
Namespaces aren't even related or even close to what you're trying to do unfortunately. While the majority of responders are right, there are some cases that you could use a hook or another clever hack to not modify a theme file - actually, there are very few cases you couldn't, just depends on how much work you want to go through, and or whether it's more of a load to just do an edit (which is no big deal), or hack it in. What code are you trying to add, and where are you wanting it to go?
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Wellwisher on April 03, 2016, 09:36:23 PM
Quote from: Matthew K. on April 03, 2016, 06:43:36 PM
Namespaces aren't even related or even close to what you're trying to do unfortunately. While the majority of responders are right, there are some cases that you could use a hook or another clever hack to not modify a theme file - actually, there are very few cases you couldn't, just depends on how much work you want to go through, and or whether it's more of a load to just do an edit (which is no big deal), or hack it in. What code are you trying to add, and where are you wanting it to go?

I need to "Add" one line code specifically to Display.template.php & Recent.template.php. Whilst I know this can easily be done using the traditional "Add/ Replace" method; I rather digg the thought of achieving this from inside MyMod.php file.  ::)

It's not like I am "Replacing" or "Modifying" existing file codes. Wouldn't it be great if  SMF developers could "Add" new codes from their mod file but prevent them from either "Replacing" or "Modifying" exisiting code? Meh, tiss an idea.  :P
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Kindred on April 03, 2016, 09:56:42 PM
No. That would be a serious security issue
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: nend on April 03, 2016, 10:05:12 PM
It really matters on where in the template the edit is needed. If it is in a function than it is harder. If you need to put the information above or under a template layer it gets a little easier.

You can also rewrite the buffer or even use JavaScript to rewrite the page, but then there is the chance the template is way different than the default template strings you are looking for.

Long story short, you might as well risk it with a template edit if your edit is right smack in the middle of a function. Allot less trouble. ;)
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: live627 on April 03, 2016, 10:22:21 PM
Quote. Wouldn't it be great if  SMF developers could "Add" new codes from their mod file but prevent them from either "Replacing" or "Modifying" exisiting code? Meh, tiss an idea.  :P
integrate_buffer?
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: margarett on April 04, 2016, 06:29:10 AM
You can't do the "add/replace" thing on the fly. It's just not possible because you would be replacing static code with dynamic operations. What if the "on-the-fly add/replace" fails for some reason? (the most obvious reason: code to search for is not found) Then you'd have most likely a crippled MOD because a part of it wouldn't be executed. Now you have at least a big read warning in Package Manager when trying to install it ;)

Hooks were originally developed for Sources (at least I think so :P ) because template are probably more difficult to expand. Says the dumb themer in me ;D
Anyway, as live627 said, considering that there are few hooks in template files, integrate_buffer is the answer.
http://wiki.simplemachines.org/smf/Integration_hooks#integrate_buffer

Check a MOD which uses it:
http://custom.simplemachines.org/mods/index.php?mod=3067
In short, before the generated HTML code is dumped to the browser, you can change it at will. Most likely you will want to perform a search for a certain (and unique!) text, then replace it (most likely with regex or str_replace if it's something simple) with itself plus and/or your MOD's generated HTML.

Not for the the faint of heart :P
Title: Re: Mod developer question: How do I replicate theme edits inside my Mod.php file?
Post by: Wellwisher on April 04, 2016, 01:23:25 PM
Quote from: margarett on April 04, 2016, 06:29:10 AM
You can't do the "add/replace" thing on the fly. It's just not possible because you would be replacing static code with dynamic operations. What if the "on-the-fly add/replace" fails for some reason? (the most obvious reason: code to search for is not found) Then you'd have most likely a crippled MOD because a part of it wouldn't be executed. Now you have at least a big read warning in Package Manager when trying to install it ;)

Hooks were originally developed for Sources (at least I think so :P ) because template are probably more difficult to expand. Says the dumb themer in me ;D
Anyway, as live627 said, considering that there are few hooks in template files, integrate_buffer is the answer.
http://wiki.simplemachines.org/smf/Integration_hooks#integrate_buffer

Check a MOD which uses it:
http://custom.simplemachines.org/mods/index.php?mod=3067
In short, before the generated HTML code is dumped to the browser, you can change it at will. Most likely you will want to perform a search for a certain (and unique!) text, then replace it (most likely with regex or str_replace if it's something simple) with itself plus and/or your MOD's generated HTML.

Not for the the faint of heart :P

Thank you @Nend for the suggestion. @Margarett Thank you too. I'll have fun tinkering around with this "integrate_buffer" hook! :laugh: I already hooked in all my functions thanks to your tutorial. I can easily achieve those two final theme edits with XML and if all else fails, I may have to stick to them.  :P But for now, I am just exploring how far I can push the mod for lol's. It's taught me a great deal, also learnt you can just have one hooks file for installing/ uninstalling, which was pretty cool.  ::)

Thanks again, much respect! 8)