MyBB Community Forums

Full Version: Convert PHP to Template Conditional?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I'm not a PHP programmer, I only know intermediate level XHTML/CSS, so bare with me if my code sucks.

I would like to convert my PHP code to a template conditional, here is what I have made below. All it does is randomly float 2 ads left or right.

<style type="text/css">
	<?php $r = rand(0,1); ?>
	<?php if($r==0) { 
	echo '.leftad336x280index {float: left;} .rightad336x280index {float: right;}'; 
	} else {
	echo '.leftad336x280index {float: right;} .rightad336x280index {float: left;}';
	}
	?>
	<?php if($r==0) { 
	echo '.leftad336x280 {float: left;} .rightad336x280 {float: right;}'; 
	} else {
	echo '.leftad336x280 {float: right;} .rightad336x280 {float: left;}';
	}
	?>
</style>

Does anyone know how this would be achieved by using Template Conditionals?
<setvar myrandomad>rand(0,1)</setvar>
<style type="text/css">
<if $tplvars['myrandomad'] == 0 then>
.leftad336x280index {float: left;}
.rightad336x280index {float: right;}
.leftad336x280 {float: left;}
.rightad336x280 {float: right;}
<else>
.leftad336x280index {float: right;}
.rightad336x280index {float: left;}
.leftad336x280 {float: right;}
.rightad336x280 {float: left;}
</if>
</style>

Make sure you uploaded the .txt file to inc/plugins from Template Conditionals package as it's required now.
Why do you guys use plugins for this? You can simply declare a variable in the Respective page (say index.php) like this:

$r = rand(0,1);

if($r==0) {
$adcodecss = '.leftad336x280index {float: left;} .rightad336x280index {float: right;}';
}
else {
$adcodecss = '.leftad336x280index {float: right;} .rightad336x280index {float: left;}';
}

simple enough? And then just use {$adcode} in your template Toungue So easy rather than using a plugin & then asking on the forums to convert code for that plugin. lol.
Quote:Why do you guys use plugins for this?
let me respond to it - that is because we do not like to modify system core files unless it is necessary.
when it is a must, to modify the system core files, we like to use a plugin (again) and that is Patches plugin
@.m.

See dude, you should avoid using plugins, what plugins do is just Add hooks to the main page, their codes get pasted on the place their hook is placed.

Modifying core files isn't a pain. If you can't workout a code correctly, just post on some PHP forum or post it here.

I don't know why MODIFYING CORE files is not upto your liking. If you fear that your code might be vulnerable due to unsanitization, you should post it somewhere for review.
Eventually by this method, you will learn also & benefit also. But when you use plugins, you become lazy as you don't need to learn anything, you just change some text in settings and boom.

That i really don't like. Anyways, it depends on people actually. That's my liking, the text above i wrote.

And I never knew people use PLUGINS to modify core files. lol.
^ I honor your views and your skills but I do not agree to above statements Smile let us stop this talk here.
(2013-07-26, 05:00 PM)Cedric Wrote: [ -> ]what plugins do is just Add hooks to the main page, their codes get pasted on the place their hook is placed.
Its in-correct, hook doesn't work like that way. Instead, the hooks used in active plugins are loaded on each MyBB page call when MyBB "includes" the plugin files. Then every time a MyBB page encounters a "run_hooks" line it looks for the in-memory list of hooks as loaded by the "add_hook" lines in the plugin files.

(2013-07-26, 05:00 PM)Cedric Wrote: [ -> ]Modifying core files isn't a pain.
It is a pain when you upgrade your forum version.
(2013-07-26, 05:00 PM)Cedric Wrote: [ -> ]I don't know why MODIFYING CORE files is not upto your liking.

Have you ever upgraded MyBB? I guess not, look how the upgrade packages are made. Then imagine you have 50 changes in each file which is in upgrade package. Unless you have so much time to look for differences in each file (I bet you wouldn't even mind editing HTML/CSS/JS in core files instead of using Stylesheet/Template system then) or don't want to upgrade (which is your individual preference not recommended by MyBB developers), there is no sense in doing core edits without Patches plugin.

(2013-07-26, 05:00 PM)Cedric Wrote: [ -> ]But when you use plugins, you become lazy as you don't need to learn anything, you just change some text in settings and boom.

Do you heal yourself when you're ill instead of going to doctor? Do you produce your own food? No? So why don't you respect other people who aren't tech-savvy or don't have time and use ready-made solutions? Of course not knowing even the basics of HTML as forum admin is a bit ridiculous in my opinion too, but why do you expect everyone to learn PHP? That's not wonderland with everyone being genius and knowing everything.
Oh, and you use ready-made PHP yourself too, if you haven't noticed yet. It's called MyBB.
I'm really interested about this topic and wondering how it work converting php to template conditional. Can anybody explain it to me? Will it help editing your mybb (in depth) to meet your best needs just by a template? (Instead of having to go in depth through the PHP files)
For example, I downloaded a plugin that will display "Who's Online" and I hate plugins because it doesn't save into your templates so when I transfer my theme to a different site, I will have to reinstall the Plugins(PHP). It would be much easier to access my PHP(After converting) through the templates and edit it to make my website more flexible. When I download plugins, I don't use it alone, the only reasons I download it is to edit the files to make it into my requirements I have planned.

Example of that is I downloaded the "Who's Online Today" plugin and I just wanted the functions and the shortcut code {$plugincode} to place into my templates where I need it.

I apologized, I read it wrong. :/ I thought there is a plugin called "Template Conditional" which convert PHP into templates which can be accessed in your admin control panel to edit. :/
(2013-07-26, 11:34 AM)Destroy666 Wrote: [ -> ]
<setvar myrandomad>rand(0,1)</setvar>
<style type="text/css">
<if $tplvars['myrandomad'] == 0 then>
.leftad336x280index {float: left;}
.rightad336x280index {float: right;}
.leftad336x280 {float: left;}
.rightad336x280 {float: right;}
<else>
.leftad336x280index {float: right;}
.rightad336x280index {float: left;}
.leftad336x280 {float: right;}
.rightad336x280 {float: left;}
</if>
</style>

Make sure you uploaded the .txt file to inc/plugins from Template Conditionals package as it's required now.

When I put this code in the post bit template, it comes up with a validation error, saying it needs to be in the <head> section.

But when I put it in the head section, it doesn't work anymore. Help!