Problems with injecting a new template into a template hook that contains a <xen:foreach loop.

Yes, I'm trying out the suggestion from ManOnDaMoon and as I'm terrible with Regex I can't get it to work :(

The parsed output of <a href="{xen:link mylink, $thread}"> would be something like <a href="index.php?mylink/thread-name.2">

The preg_replace needs to be something like:

PHP:
$pattern = '/<li id="thread-' . $thread['thread_id'] . '" class=(.+)<div class="controls faint">(.+)<a href="/'
 
$replace = ????????;
 
$contents = preg_replace($pattern, $replace, $contents);
 
Yes, I'm trying out the suggestion from ManOnDaMoon and as I'm terrible with Regex I can't get it to work :(

The parsed output of <a href="{xen:link mylink, $thread}"> would be something like <a href="index.php?mylink/thread-name.2">

The preg_replace needs to be something like:

PHP:
$pattern = '/<li id="thread-' . $thread['thread_id'] . '" class=(.+)<div class="controls faint">(.+)<a href="/'
 
$replace = ????????;
 
$contents = preg_replace($pattern, $replace, $contents);
Arf... you will use then url rewriting in your url. I guess it will be easier then to use a template with xen code and not direct php. I'll give a look for the regex.
 
Why would I need url rewriting?

Part of my replacement is just going to be my rendered template so the {xen:link tags will render correctly. Even if I have to do some dodgy HTML in the Listener then I could just use the XenForo_Link class.
 
Why would I need url rewriting?

Part of my replacement is just going to be my rendered template so the {xen:link tags will render correctly. Even if I have to do some dodgy HTML in the Listener then I could just use the XenForo_Link class.
May be i used the wrong name, but it's because you're using an url that won't be the same if XenForo Friendly Url option is checked.

Here is a working regex:

PHP:
        if($hookName == 'thread_list_threads')
        {
            $threads = $template->getParam('threads');
            $exclude = 'myid_' . uniqid(); //Let's create an exclusion for the regex (needs to be deleted at the end)
   
            foreach ($threads as $thread)
            {
                $threadid = $thread['thread_id'];
                $contents = preg_replace('#(?<!' . $exclude . ')<div.+?class="(?:[^"]*?)?controls(?:[^"]*?)?faint(?:[^"]*?)?"(?:[^"]*?)?>.*#ui', $exclude . "$0 test-$threadid", $contents, 1);
            }
 
            //Clean regex exclusion from template
            $contents = str_replace($exclude, '', $contents);
        }

You will need to replace this part(...):
PHP:
$exclude . "$0 test-$threadid"
(...) with your template inside which you can now have the $thread variable that will match the correct part of the code. Just do not delete $exclude and the $0 at the begin of your replacement. They must come before your template code.

The final code should be something like this:
PHP:
        if($hookName == 'thread_list_threads')
        {
            $threads = $template->getParam('threads');
            $exclude = 'myid_' . uniqid(); //Let's create an exclusion for the regex (needs to be deleted at the end)
         
            foreach ($threads as $thread)
            {
                $wip = $template->create('yourCustomTemplate', array('thread' => $thread));
                $replace = $exclude . '$0' . $wip->render();
                $contents = preg_replace('#(?<!' . $exclude . ')<div.+?class="(?:[^"]*?)?controls(?:[^"]*?)?faint(?:[^"]*?)?"(?:[^"]*?)?>.*#ui', $replace, $contents, 1);
            }
     
            //Clean regex exclusion from template
            $contents = str_replace($exclude, '', $contents);
        }

P.S: I'm still thinking the tms would be easier to deal with this ;)
 
Cédric, I love you :love:

That works great, thank you.

URLs aren't a problem as I have used XenForo_Link::buildPublicLink which adheres to the type of URLs you have configured on the board. A few minor adjustments and it's working PERFECTLY :D

Thank you so much.

And thank you to everyone else who helped. You're all awesome (y)

EDIT: And yes, TMS would be better. But after the trouble I've gone through to get here so far I like this way! :D
 
Top Bottom