Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 05-15-2013, 11:34 AM
emath emath is offline
 
Join Date: Sep 2008
Posts: 252
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default str_replace problem

im trying to relpace a two rows in SHOWTHREAD template,

this is my code :

PHP Code:
if ( $vbulletin->options['active_users_onoff'] ) {
    
$replace '                        <!-- Refresh Button -->
                            <vb:if condition="$vbulletin->options[\'active_users_onoff\']">
                            <button onclick="showUsers()" type="button" style="float:left;"> {vb:rawphrase refresh_button} </button>
                            </vb:if>
                <div id="thread_active_users">' 
PHP_EOL '                <p>' ;
    
vB_Template::preRegister('SHOWTHREAD',array('replace' => $replace));
    
$vbulletin->templatecache['SHOWTHREAD'] = str_replace('<div>' PHP_EOL '                <p>''\'' $replace '\'' ,$vbulletin->templatecache['SHOWTHREAD']); 

in the template, there is a <div>, new line, some space and <p> . this is what i would like to change... but the above hook doesnt work ( placed in showthread_complete )

Edit : tried also with "\n" instead of PHP_EOL with no success .


thanks
Reply With Quote
  #2  
Old 05-15-2013, 12:09 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

$vbulletin->templatecache contains the compiled template, so you probably won't be able to figure out what to match when doing a str_replace() by looking at the template in the template editor (except maybe in a case where you're trying to match a small piece of html). Also, you cannot use template tags or curly braces in your replacement since it's already been compiled. So I think there are two things you can do: figure out the right replacement for the compiled template, or do a replacement after it's been rendered.

You can look in the database "template" table, and the "template" column contains the compiled version of the template (or you can put code in a plugin to print out what's in $vbulletin->templatecache). The cached version of a template is php code that sets a $final_rendered variable when it's eval()'d. You can figure out a str_replace() for that, but it can be tricky to get the quotes right.

If you want to try the second method, you could use hook global_complete and do str_replace() on the $output variable (and probably check for THIS_SCRIPT == 'showthread' so you're not doing it for every page). For this method you can look at the html source of the page in your browser to figure out the replacement.

ETA: Also, I don't think you need the call to vB_Template:reRegister().
Reply With Quote
Благодарность от:
emath
  #3  
Old 05-15-2013, 01:57 PM
emath emath is offline
 
Join Date: Sep 2008
Posts: 252
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

the first option is not good cause i need to change a <div> tag and i cant find such in the compiled template .

about this code :

PHP Code:
 if ( $vbulletin->options['active_users_onoff'] ) {
    
$jsvar vB_Template::create('active_users_js')->render();
    
vB_Template::preRegister('SHOWTHREAD',array('jsvar' => $jsvar));
    
$vbulletin->templatecache['SHOWTHREAD'] = str_replace('<head>','<head> \' . $jsvar . \' ',$vbulletin->templatecache['SHOWTHREAD']); 

1. it worked for me, why is that if i used the <head> to do the replace ?

2. why here i must use preRegister .

3. tried this on global_complete with no success at all :

PHP Code:
if ( $vbulletin->options['active_users_onoff'] AND THIS_SCRIPT=='SHOWTHREAD' ) {
    
$replace '                        <!-- Refresh Button -->
                            <vb:if condition="$vbulletin->options[\'active_users_onoff\']">
                            <button onclick="showUsers()" type="button" style="float:left;"> {vb:rawphrase refresh_button} </button>
                            </vb:if>
                <div id="thread_active_users">' 
PHP_EOL '                <p>' ;
    
// vB_Template::preRegister('SHOWTHREAD',array('replace' => $replace));
    
$output str_replace('<p>''\'' $replace '\'' ,$output); 

of course i need to change the div also but i kept it with the <p> just to see if there is a change.
also, on global_complete can i use {vb:rawphrase.. ?

after its compiled - its a php code. after its rendered - does it includes the {vb:rawphr... or puts a text instead already ?

Thanks alot for the help .
Reply With Quote
  #4  
Old 05-15-2013, 02:39 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by emath View Post
1. it worked for me, why is that if i used the <head> to do the replace ?
Well, like I mentioned, if you're only looking for a simple piece of html then that's OK. So maybe the code you originally posted would be able to match if you figured out the correct string (which I guess is what you were asking in the first place). It still may be helpful to look at the compiled template and find that section, and see what needs to be matched. It could be that the newline is not in the compiled template at all.

In any case, what your replacing is inside a php single quoted string. So your replacement can be only html. (Well, it could actually be any php code you wanted, as long as the result of your replacement ends up as valid php). If your replacement contains any single quote characters, they would have to be escaped with backslashes or else they would cause an error when the template is rendered.

Quote:
2. why here i must use preRegister .
It depends on exactly what's in the template cache after the replacement is done. You only need the preRegister if resulting php code uses that variable. In the code you posted above, the replacement inserts the $jsvar variable so that it's evaluated when the template is rendered, so the preRegister is required. It might have been possible to insert the actual value of $jsvar instead of the variable (if the value is known at the time of the replacement), then preRegister wouldn't be needed.


Quote:
3. tried this on global_complete with no success at all :
It seems like that should have done something if there's at least one <p> tag in the page somewhere. Also, again your replacement cannot contain template tags. or {vb:raw.., {vb:rawphrase.., etc. So maybe a replacement did happen but didn't display anything on the page.

Quote:
after its compiled - its a php code. after its rendered - does it includes the {vb:rawphr... or puts a text instead already ?
When the template is compiled (before you do your replace on it), {vb:raw... is replaced with a variable, and {vb:rawphrase...} is replaced with the phrase text (the contents of $vbphrase['phrasename'], or a call to construct_phrase() if there are parameters).

In short, you need to do the work of the template compiler before you do the replacement, but it's not difficult because the template tags just get converted to php code. For the replacement you posted above, you coudl try something like this:

Code:
$find = '<div id="thread_active_users"><p>' ;
$replace = "                        <!-- Refresh Button -->\n";
if ($vbulletin->options['active_users_onoff'])
   $replace .= '<button onclick="showUsers()" type="button" style="float:left;"> ' . htmlspecialchars_uni($vbphrase['refresh_button']) . '</button>';
$replace .= $find;
$vbulletin->templatecache['SHOWTHREAD'] = str_replace($find, $replace, $vbulletin->templatecache['SHOWTHREAD']);

(I made the assumption that the newline is just missing, but like I mentions above, you might have to look at the compiled template to figure out what $find should be). The call to htmlspecialchars_uni() should take care of any quotes or special characters that might be in the phrase text.

ETA: Actually, now that I look at the above again, I guess you'd only want to do the replacement if the options was true, so something like this may be better:

Code:
if ($vbulletin->options['active_users_onoff'])
{
   $find = '<div id="thread_active_users"><p>' ;
   $replace = '                        <!-- Refresh Button -->
<button onclick="showUsers()" type="button" style="float:left;"> ' . htmlspecialchars_uni($vbphrase['refresh_button']) . '</button>' . $find;
   $vbulletin->templatecache['SHOWTHREAD'] = str_replace($find, $replace, $vbulletin->templatecache['SHOWTHREAD']);
}
Reply With Quote
  #5  
Old 05-15-2013, 03:17 PM
emath emath is offline
 
Join Date: Sep 2008
Posts: 252
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

thats looks very helpful though it still not what i need .

i want to change the <div> tag to <div id="thread_active_users">

actually,

the code in the SHOWTHREAD template is :

Code:
			<div>
				<p>
and i want to change it with :

Code:
                        <!-- Refresh Button -->
							<vb:if condition="$vbulletin->options[\'active_users_onoff\']">
							<button onclick="showUsers()" type="button" style="float:left;"> {vb:rawphrase refresh_button} </button>
							</vb:if>
				<div id="thread_active_users">
				<p>
so i can use what u did and do something like that :

PHP Code:
$find '<div>' "\n" '                <p>';
$replace "                        <!-- Refresh Button -->\n";
if (
$vbulletin->options['active_users_onoff'])
   
$replace .= '<button onclick="showUsers()" type="button" style="float:left;"> ' htmlspecialchars_uni($vbphrase['refresh_button']) . '</button>' '<div id="thread_active_users">' "\n" '                <p>';
// $replace .= $find;
$vbulletin->templatecache['SHOWTHREAD'] = str_replace($find$replace$vbulletin->templatecache['SHOWTHREAD']); 

though this on the hook showthread_complete doesnt work.

--------------- Added [DATE]1368635293[/DATE] at [TIME]1368635293[/TIME] ---------------

how can i find the right text to find? the compiled version of SHOWTHREAD doesnt help at all...

this is the right part of it i think, but it tells me nothing :

Code:
' . vB_Template_Runtime::parsePhrase("users_browsing_this_thread") . '

' . vB_Template_Runtime::parsePhrase("users_currently_browsing_x_y_z", '' . $totalonline . '', '' . $numberregistered . '', '' . $numberguest . '') . '
Reply With Quote
  #6  
Old 05-15-2013, 03:29 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I think your problem might be the white space. Looking at the html source for the showthread page on my test server, it looks like there's a newline followed by 4 tabs, so you might try "\n\t\t\t\t". And if that doesn't work, maybe try "\r\n\t\t\t\t".
Reply With Quote
  #7  
Old 05-15-2013, 03:50 PM
emath emath is offline
 
Join Date: Sep 2008
Posts: 252
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
$find '<div>' "\r\n\t\t\t\t" '<p>' ;
$replace "                        <!-- Refresh Button -->\n";
if (
$vbulletin->options['active_users_onoff'])
   
$replace .= '<button onclick="showUsers()" type="button" style="float:left;"> ' htmlspecialchars_uni($vbphrase['refresh_button']) . '</button>' '<div id="thread_active_users">' "\n" '                <p>';
// $replace .= $find;
$vbulletin->templatecache['SHOWTHREAD'] = str_replace($find$replace$vbulletin->templatecache['SHOWTHREAD']); 
did the job ! Thanks !!!
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:37 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.07012 seconds
  • Memory Usage 2,271KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (5)bbcode_code
  • (5)bbcode_php
  • (4)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (1)post_thanks_box_bit
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete