PDA

View Full Version : "Compiled template"


Scanu
12-19-2011, 08:33 PM
Hi everybody! :) Let's start
Here what kh99

Unfortunately it's difficult, I could write pages and still not explain everything (partly because I don't fully understand it myself). But in this case I happen to have a mod that changes the SHOWTHREAD template. It uses the showthread_getinfo hook location and has code like this
$find = "some string";
$replace = "other string";
$vbulletin->templatecache['SHOWTHREAD'] = str_replace($find, $replace, $vbulletin->templatecache['SHOWTHREAD']);


but, $vbulletin->templatecache contains compiled templates so it doesn't look like what you see in the template editor. That makes it tricky to know what you can search for and match. You could look in the template table in the database, the "template" column shows the compiled template. Or what I usually do is put in temporary code to write the cached template string to a file, like

$fp = fopen("showthread.txt", "wb");
fwrite($fp, $vbulletin->templatecache['SHOWTHREAD']);


Then I open that file in my editor so I can refer to it.

I hope that makes some sense.

i need to know what's the code of the compiled template "forumhome_forumbit_level2_post"

I tried your code (FOR SHOWTHREAD) and it gets me a blank text file, so nothing

:( This is the code that i need to "convert"


<vb:comment><h2 class="forumtitle"><a href="{vb:link forum, {vb:raw forum}}">{vb:raw forum.title}</a></h2></vb:comment>
<h2 class="forumtitle"><div id="scanu"><a <vb:if condition="$show[\'forumdescription\']">title=\'<scanu>{vb:raw forum.description}</scanu>\'</vb:if> href="{vb:link forum, {vb:raw forum}}">{vb:raw forum.title}</a></div></h2>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://cdn.jquerytools.org/1.2.6/all/jquery.tools.min.js"></script>
<script>
$("#scanu a[title]").tooltip({
position: \'center right\',
effect: \'slide\'
});
</script>
<style>
scanu {
background:black;
color:white;
padding: 5px 10px;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
filter:alpha(opacity=68);
-moz-opacity:.68;
opacity:.68;
}
</style>


how can i do? :)
thanks in advance :)

--------------- Added 1324331782 at 1324331782 ---------------

I see the database and i've tried this code

$find = '<h2 class="forumtitle"><a href="' . vB_Template_Runtime::linkBuild("forum", $forum) . '">' . $forum['title'] . '</a></h2>';
$replace = '
<h2 class="forumtitle"><div id="scanu"><a title=\'<scanu>' . $forum['description'] . '</scanu>\' href="' . vB_Template_Runtime::linkBuild("forum", $forum) . '">' . $forum['title'] . '</a></div></h2>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://cdn.jquerytools.org/1.2.6/all/jquery.tools.min.js"></script>
<script>
$("#scanu a[title]").tooltip({
position: \'center right\',
effect: \'slide\'
});
</script>
<style>
scanu {
background:black;
color:white;
padding: 5px 10px;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
filter:alpha(opacity=68);
-moz-opacity:.68;
opacity:.68;
}
</style>';
$vbulletin->templatecache['forumhome_forumbit_level2_post'] = str_replace($find, $replace, $vbulletin->templatecache['forumhome_forumbit_level2_post']);


But didn't work :(

--------------- Added 1324332260 at 1324332260 ---------------


$find = '' . $forum['title'] . '';
$replace = '';
$vbulletin->templatecache['forumhome_forumbit_level2_post'] = str_replace($find, $replace, $vbulletin->templatecache['forumhome_forumbit_level2_post']);

This code would be delete the title of the forum but doesn't work! This code instead that would be delete the text "test" without variables works D:

$find = 'test';
$replace = '';
$vbulletin->templatecache['forumhome_forumbit_level2_post'] = str_replace($find, $replace, $vbulletin->templatecache['forumhome_forumbit_level2_post']);

kh99
12-19-2011, 09:10 PM
The reason the code using the showthread hook didn't work for you is that not every template is loaded on every page, and the forumbit templates aren't loaded on the showthread page. But as you figured out, you can look in the database and find the same info.

I'm not sure if you're saying you figured it out or you still have a problem.

Scanu
12-19-2011, 09:20 PM
ok i try the code of the showthread for the showthread exactly as you said just for see, but doesn't work. then yesy i see the code in the databse and i tried this code

$find = '<h2 class="forumtitle"><a href="' . vB_Template_Runtime::linkBuild("forum", $forum) . '">' . $forum['title'] . '</a></h2>';
$replace = '
<h2 class="forumtitle"><div id="scanu"><a title=\'<scanu>' . $forum['description'] . '</scanu>\' href="' . vB_Template_Runtime::linkBuild("forum", $forum) . '">' . $forum['title'] . '</a></div></h2>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://cdn.jquerytools.org/1.2.6/all/jquery.tools.min.js"></script>
<script>
$("#scanu a[title]").tooltip({
position: \'center right\',
effect: \'slide\'
});
</script>
<style>
scanu {
background:black;
color:white;
padding: 5px 10px;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
filter:alpha(opacity=68);
-moz-opacity:.68;
opacity:.68;
}
</style>';
$vbulletin->templatecache['forumhome_forumbit_level2_post'] = str_replace($find, $replace, $vbulletin->templatecache['forumhome_forumbit_level2_post']);

and so i'm asking to me why? :S

kh99
12-19-2011, 09:23 PM
OK, try this:

$find = '<h2 class="forumtitle"><a href="\' . vB_Template_Runtime::linkBuild("forum", $forum) . '">\' . $forum['title'] . \'</a></h2>';
$replace = '
<h2 class="forumtitle"><div id="scanu"><a title=\'<scanu>' . $forum['description'] . '</scanu>\' href="' . vB_Template_Runtime::linkBuild("forum", $forum) . '">' . $forum['title'] . '</a></div></h2>


Edit: well, the replace code there isn't right, but the point is that you need to escape all single quotes in the string you're trying to match. You seem to have escaped the quotes in the string you're adding, but not in the string you got from the compiled template.

Scanu
12-20-2011, 09:48 AM
What!? The code didn't worked but i've changed this
' . $forum['title'] . '
in this:
' . $forum[\'title\'] . '
And it worked just this code with this code

$find = '\' . $forum[\'title\'] . \'';
$replace = '';
$vbulletin->templatecache['forumhome_forumbit_level2_post'] = str_replace($find, $replace, $vbulletin->templatecache['forumhome_forumbit_level2_post']);

Now i will try all the first code :)

--------------- Added 1324383827 at 1324383827 ---------------

Everything work but there is a problem: This is my code

$find = '<h2 class="forumtitle"><a href="\' . vB_Template_Runtime::linkBuild("forum", $forum) . \'">\' . $forum[\'title\'] . \'</a></h2>';
$replace = '<h2 class="forumtitle"><div id="scanu"><a title="<scanu>\' . $forum[\'description\'] . \'</scanu>" href="\' . vB_Template_Runtime::linkBuild("forum", $forum) . \'">\' . $forum[\'title\'] . \'</a></div></h2>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://cdn.jquerytools.org/1.2.6/all/jquery.tools.min.js"></script>
<script>
$("#scanu a[title]").tooltip({
position: "center right",
effect: "slide"
});
</script>
<style>
scanu {
background:black;
color:white;
padding: 5px 10px;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
filter:alpha(opacity=68);
-moz-opacity:.68;
opacity:.68;
}
</style>';


$vbulletin->templatecache['forumhome_forumbit_level2_post'] = str_replace($find, $replace, $vbulletin->templatecache['forumhome_forumbit_level2_post']);


If i change this

title="<scanu>\' . $forum[\'description\'] . \'</scanu>"

in this

title=\'<scanu>\' . $forum[\'description\'] . \'</scanu>\'

doesn't work! Why? :(

kh99
12-20-2011, 01:09 PM
I believe html attributes need to have " and not '.

Scanu
12-20-2011, 01:53 PM
No that's a choice of the coder (as php) in fact in normal html works but in this code not. As i change " to \' it delete all the template o.o. I need that is ' and not "
In php syntax this would be work :( That's strange

kh99
12-20-2011, 02:03 PM
Oh, OK - I don't do enough html to keep that stuff straight. But I think then the reason it doesn't work is that it ends up producing a php string where the quotes are unmatched (or matched but not the way you intended). This stuff gets really confusing and hard to keep track of in my mind, but I think if you did this is might work:

title=\\'<scanu>\' . $forum[\'description\'] . \'</scanu>\\'

Scanu
12-20-2011, 02:18 PM
Oh, OK - I don't do enough html to keep that stuff straight. But I think then the reason it doesn't work is that it ends up producing a php string where the quotes are unmatched (or matched but not the way you intended). This stuff gets really confusing and hard to keep track of in my mind, but I think if you did this is might work:

title=\\'<scanu>\' . $forum[\'description\'] . \'</scanu>\\'

That's the same for me, anyway it doesn't work :( Very strange!
I tried " and it worked but not as i want
I tried \' and it doesn't work
Now i tried \\' and it still doesn't work

Now the last thing i can do is change this
$replace = ' ';
on
$replace = " ";
and so i can leave title = 'etc...' without problems, i have just to place a \ before the "

--------------- Added 1324395032 at 1324395032 ---------------

Worst! Damn this code is perfect!

$find = '<h2 class="forumtitle"><a href="\' . vB_Template_Runtime::linkBuild("forum", $forum) . \'">\' . $forum[\'title\'] . \'</a></h2>';
$replace = '<h2 class="forumtitle"><div id="scanu"><a title="<scanu>\' . $forum[\'description\'] . \'</scanu>" href="\' . vB_Template_Runtime::linkBuild("forum", $forum) . \'">\' . $forum[\'title\'] . \'</a></div></h2>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://cdn.jquerytools.org/1.2.6/all/jquery.tools.min.js"></script>
<script>
$("#scanu a[title]").tooltip({
position: "center right",
effect: "slide"
});
</script>
<style>
scanu {
background:black;
color:white;
padding: 5px 10px;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
filter:alpha(opacity=68);
-moz-opacity:.68;
opacity:.68;
}
</style>';


$vbulletin->templatecache['forumhome_forumbit_level2_post'] = str_replace($find, $replace, $vbulletin->templatecache['forumhome_forumbit_level2_post']);

Why i can't change " to \' ? :(

kh99
12-20-2011, 03:24 PM
Maybe try using "heredoc" syntax for the replacement string: http://us2.php.net/manual/en/language.types.string.php

Scanu
12-20-2011, 04:19 PM
Well that's a little more difficult i'm thinking about this code

$forum['title'] = str_replace('"', "'", $forum['title']);

what would be the hook location?
*replace the " with the '

--------------- Added 1324406159 at 1324406159 ---------------

Ok this is driving me insane o.o! I can't put a ' in a string!!
Is vbulletin?! Why this code doesn't work? :(

$find = '<h2 class="forumtitle"><a href="\' . vB_Template_Runtime::linkBuild("forum", $forum) . \'">\' . $forum[\'title\'] . \'</a></h2>';
$replace = ' \' ';


$vbulletin->templatecache['forumhome_forumbit_level2_post'] = str_replace($find, $replace, $vbulletin->templatecache['forumhome_forumbit_level2_post']);

If i write " all is ok but if i write \' it delete all the template! This is impossible..

--------------- Added 1324418531 at 1324418531 ---------------

Ok i'm thinking that this template doesn't accepts apostrophe i tried this code and it works

$find = 'test';
$replace = <<<DH DH;
$vbulletin->templatecache['forumhome_forumbit_level2_post'] = str_replace($find, $replace, $vbulletin->templatecache['forumhome_forumbit_level2_post']);

Then i tried this code but doesn't works!

$find = 'test';
$replace = <<<DH ' DH;
$vbulletin->templatecache['forumhome_forumbit_level2_post'] = str_replace($find, $replace, $vbulletin->templatecache['forumhome_forumbit_level2_post']);

What i mean with "doesn't work" it delete the template

kh99
12-20-2011, 09:44 PM
To be honest, I'm not sure what you're trying to do exactly. Are you tryig to delete the entire $find string? And why are you trying to replace it with a ' ?

Scanu
12-20-2011, 09:57 PM
No these are test for see what's the problem, and the problem is that if i put a ' in the template, all the template is eliminated, so the problem is why i can't put a ' in a template!? Is a vbulletin problem

kh99
12-20-2011, 10:02 PM
I think you need to figure out the right 2 levels of "escaping". Maybe try this:

$replace = "\\'";

Scanu
12-20-2011, 10:06 PM
Nothing, i think that is a problem of vbulletin, because in php all it's right and works

kh99
12-20-2011, 10:22 PM
But the template string is php code, and if you change it so that the quotes are mismatched the template will go away because when it's evaluated it will produce an error. So I think it's just that you're not ending up with valid php code.

Scanu
12-21-2011, 09:21 AM
Yes this is possible but the question is why the this is not a valid php code!? I mean why this works
$find = 'test';
$replace = " ";
$vbulletin->templatecache['forumhome_forumbit_level2_post'] = str_replace($find, $replace, $vbulletin->templatecache['forumhome_forumbit_level2_post']);
And this doesn't work
$find = 'test';
$replace = " ' ";
$vbulletin->templatecache['forumhome_forumbit_level2_post'] = str_replace($find, $replace, $vbulletin->templatecache['forumhome_forumbit_level2_post']);

kh99
12-21-2011, 09:56 AM
I guess it depends where test is in the template, but you're adding one single quote, so wouldn't it mess up the quote matching?

Scanu
12-21-2011, 10:18 AM
Test can be located wherever, what do you mean with "you're adding one single quote, so wouldn't it mess up the quote matching"
The string is declared by " and so i can write " ' "

kh99
12-21-2011, 10:26 AM
The templatecache string uses single quotes, so unless you are careful where you put it, you're probably creating mismatched quotes.

ETA: unless you're saying that test that you're replacing is in a double-quoted string? That's why I asked where it was. Do you have a way to print out the templatecache string so you can see it? I think if you print it out before and after your replace, you'll probably be able to see what the problem is.

Scanu
12-21-2011, 11:58 AM
Aaah ok yes understand but i don't know how to print out the templatecache string o.o

--------------- Added 1324472883 at 1324472883 ---------------

Mmh interesting, i was in the "forumhome_forumbit_level2_post" template and at the end of the code i add an apostrophe (') then in the forumhome appears 4 apostrophe (''''). Is that a bug?