News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Mod developer question: How do I replicate theme edits inside my Mod.php file?

Started by Wellwisher, March 30, 2016, 01:34:39 PM

Previous topic - Next topic

Wellwisher

I have "hooked" in all my functions (thanks to @Margarett's hooks tutorial & advise from @Jblaze, @Nend & @Shambles - yay!).
Feels AWESOME8) 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>

JBlaze

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.
Jason Clemons
Former Team Member 2009 - 2012

Wellwisher

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?

JBlaze

What exactly is failing? Is it giving an error on install, or is it not adding the actual code to the template?
Jason Clemons
Former Team Member 2009 - 2012


Illori

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.

Wellwisher

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. :-\

Illori

many mods do this. check the mods on the mod site otherwise we cant tell you what may be going wrong.

Wellwisher

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)?

JBlaze

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.
Jason Clemons
Former Team Member 2009 - 2012

Wellwisher

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.

Illori

yes many mods add edits to the install.xml file for theme edits... that is what you asked about.

Wellwisher

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.  ::)

JBlaze

Jason Clemons
Former Team Member 2009 - 2012




Matthew K.

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?

Wellwisher

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

Kindred

Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

nend

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. ;)

live627

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?

margarett

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
Se forem conduzir, não bebam. Se forem beber... CHAMEM-ME!!!! :D

QuoteOver 90% of all computer problems can be traced back to the interface between the keyboard and the chair

Wellwisher

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)

Advertisement: