Current time: 05-23-2012, 10:04 PM Hello There, Guest! (LoginRegister)


Post Reply 
 
Thread Rating:
  • 15 Votes - 3.73 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PHP in Templates and Template Conditionals
06-28-2009, 08:40 AM (This post was last modified: 06-28-2009 08:53 AM by jnd52.)
Post: #41
RE: PHP in Templates and Template Conditionals
Hey everyone!

I am having trouble using php and conditionals in my templates. It used to work like a charm, but for some reason it stopped working. I am running MyBB 1.2.14 on www.maroonlife.com.

I am trying to add the following code to my postbit template:
Code:
<!--Testing Share--->
<tr align="right" valign="bottom">
<span class="smalltext">
<div align="right" id="sharing">
<img src="http://static.ak.fbcdn.net/images/share/facebook_share_icon.gif">&nbsp;<a href="http://www.facebook.com/share.php?u=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>" target="_blank">Share on Facebook</a>
&nbsp;
<img border=0 src="http://planetzope.org/static/icon_delicious.gif" alt="" width="16" height="16">&nbsp;<a href="http://del.icio.us/post?v=2&url=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>&title=<func urlencode>{$post['subject']}</func>&title="Post to del.icio.us">Post to del.icio.us</a>
&nbsp;
<img src="http://digg.com/img/badges/16x16-digg-guy.png" width="16" height="16" alt="Digg!" />&nbsp;<a href="http://digg.com/submit?url=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>&title=<func urlencode>{$post['subject']}</func>&bodytext=<func urlencode>{$post['message']}</func>"  TARGET="_blank">Digg This!</a>
&nbsp;
<img src="http://x.myspace.com/images/myspace_logo_16.gif"  alt="Post to MySpace!" />&nbsp;<a href="http://www.myspace.com/Modules/PostTo/Pages/?t=<func urlencode>{$post['subject']}</func>&c=<func urlencode>{$post['message']}</func>&u=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>" TARGET="_blank">Post to MySpace</a>
&nbsp;
<img src="{$mybb->settings['bburl']}/{$theme['imgdir']}/send.gif" alt="" />&nbsp;<a href="{$mybb->settings['bburl']}/sendthread.php?tid={$tid}">Email This!</a>

</div></span></tr>
<!---End Testing Share-->

Here is a copy of my postbit template:
Code:
<tr>
<td class="{$altbg}" width="15%" valign="top" style="white-space: nowrap; text-align: center;"><a name="pid{$post['pid']}" id="pid{$post['pid']}"></a>
{$post['user_details']}
</td>
<td class="{$altbg}" width="85%" valign="top">
<table width="100%">
<tr><td>{$post['posturl']}{$post['icon']}<span class="smalltext"><strong> {$post['subject']}</strong></span>
<br />
<div id="pid_{$post['pid']}">
<p>
{$post['message']}
</p>
</div>
{$post['attachments']}
{$post['signature']}
<div style="text-align: right; vertical-align: bottom;">
{$post['editedmsg']}
{$post['iplogged']}
</div>
<!--Testing Share--->
<tr align="right" valign="bottom">
<span class="smalltext">
<div align="right" id="sharing">
<img src="http://static.ak.fbcdn.net/images/share/facebook_share_icon.gif">&nbsp;<a href="http://www.facebook.com/share.php?u=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>" target="_blank">Share on Facebook</a>
&nbsp;
<img border=0 src="http://planetzope.org/static/icon_delicious.gif" alt="" width="16" height="16">&nbsp;<a href="http://del.icio.us/post?v=2&url=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>&title=<func urlencode>{$post['subject']}</func>&title="Post to del.icio.us">Post to del.icio.us</a>
&nbsp;
<img src="http://digg.com/img/badges/16x16-digg-guy.png" width="16" height="16" alt="Digg!" />&nbsp;<a href="http://digg.com/submit?url=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>&title=<func urlencode>{$post['subject']}</func>&bodytext=<func urlencode>{$post['message']}</func>"  TARGET="_blank">Digg This!</a>
&nbsp;
<img src="http://x.myspace.com/images/myspace_logo_16.gif"  alt="Post to MySpace!" />&nbsp;<a href="http://www.myspace.com/Modules/PostTo/Pages/?t=<func urlencode>{$post['subject']}</func>&c=<func urlencode>{$post['message']}</func>&u=<func urlencode>{$mybb->settings['bburl']}/showthread.php?tid={$tid}</func>" TARGET="_blank">Post to MySpace</a>
&nbsp;
<img src="{$mybb->settings['bburl']}/{$theme['imgdir']}/send.gif" alt="" />&nbsp;<a href="{$mybb->settings['bburl']}/sendthread.php?tid={$tid}">Email This!</a>

</div></span></tr>
<!---End Testing Share-->
</td></tr>
</table>
</td>
</tr>
<tr>
<td class="{$altbg}" height="18" style="white-space: nowrap; text-align: center;"><span class="smalltext">{$post['postdate']} {$post['posttime']}</span></td>
<td class="{$altbg}" width="100%" valign="middle" height="18">
    <table width="100%" border="0" cellpadding="0" cellspacing="0">
    <tr valign="bottom">
        <td align="left" ><span class="smalltext">{$post['button_email']}{$post['button_pm']}{$post['button_www']}{$post['button_find']}</span></td>
        <td align="right"><span class="smalltext">{$post['button_edit']}{$post['button_quickdelete']}{$post['button_quote']}{$post['button_multiquote']}{$post['button_report']}</span></td>
    </tr>
    </table>
</td>
</tr>
{$seperator}


Here is a picture of one of the posts.
   

After deactivating and reactivating multiple times, I tried to use the simple example:
PHP Code:
<?php echo "Hi from PHP"?>

No luck though, it did not work. So I deactivated it and redownloaded it and reactivated it, still no luck.

Here is the code for the plugin:
PHP Code:
<?php

/*
 *  Known issue: in PHP evaluation, "?>" may not match properly if used in strings
 */

if(!defined('IN_MYBB'))
    die(
'This file cannot be accessed directly.');

if(!
defined('IN_ADMINCP'))
{
    
// gain control of $templates object
    
    
eval('
        class phptpl_templates extends '
.get_class($GLOBALS['templates']).'
        {
            function phptpl_templates(&$oldtpl)
            {
                $vars = get_object_vars($oldtpl);
                foreach($vars as $var => $val)
                    $this->$var = $val;
                
                $this->def_htmlcomments = $GLOBALS[\'mybb\']->settings[\'tplhtmlcomments\'];
                $this->def_htmlcomments = (($this->def_htmlcomments == \'yes\' || $this->def_htmlcomments == 1) ? 1:0);
            }
            function get($title, $eslashes=1, $htmlcomments=1)
            {
                if($eslashes && $this->def_htmlcomments == $htmlcomments)
                {
                    if(!isset($this->parsed_cache[$title]))
                    {
                        $this->parsed_cache[$title] = parent::get($title, $eslashes, $htmlcomments);
                        // parse the template
                        phptpl_parsetpl($this->parsed_cache[$title]);
                    }
                    
                    return $this->parsed_cache[$title];
                }
                else
                    return parent::get($title, $eslashes, $htmlcomments);
            }
        }
    '
);
    
$GLOBALS['templates'] = new phptpl_templates($GLOBALS['templates']);
}

function 
phptpl_info()
{
    return array(
        
'name'            => 'PHP and Template Conditionals',
        
'description'    => 'Allows you to use conditionals and PHP code in templates.',
        
'website'        => 'http://mybbhacks.zingaburga.com/',
        
'author'        => 'ZiNgA BuRgA',
        
'authorsite'    => 'http://zingaburga.com/',
        
'version'        => '1.3',
        
'compatibility'    => '1*',
        
'guid'            => ''
    
);
}

function 
phptpl_parsetpl(&$ourtpl)
{
    
$ourtpl preg_replace(array(
        
'#\<\?.+?(\?\>)#se'// '#\<\?.*?(\?\>|$)#se',
        
'#\<if (.*?) then\>#sie',
        
'#\<elseif (.*?) then\>#sie',
        
'#\<else( /)?\>#i',
        
'#\</if\>#i',
        
'#\<func (htmlspecialchars|htmlspecialchars_uni|intval|file_get_contents|floatval|ur​lencode|rawurlencode|addslashes|stripslashes|trim|crc32|ltrim|rtrim|md5|nl2br|sh​a1|strrev|strtoupper|strtolower|my_strtoupper|my_strtolower|alt_trow|get_friendl​y_size|filesize|strlen|my_strlen|my_wordwrap|random_str|unicode_chr)\>#i',
        
'#\</func\>#i'
    
), array(
        
'phptpl_evalphp(\'$0\', \'$1\')',
        
'\'".phptpl_iif(\'.phptpl_unescape_string(\'$1\').\',"\'',
        
'\'",\'.phptpl_unescape_string(\'$1\').\',"\'',
        
'","',
        
'")."',
        
'".$1("',
        
'")."'
    
), $ourtpl);
}


// unescapes the slashes added by $templates->get(), plus addslashes() during preg_replace()
function phptpl_unescape_string($str)
{
    return 
strtr($str, array('\\\\"' => '"''\\\\' => '\\'));
}

function 
phptpl_evalphp($str$end)
{
    return 
'".eval(\'ob_start(); ?>'
        
.strtr(phptpl_unescape_string($str), array('\'' => '\\\'''\\' => '\\\\'))
        .(
$end?'':'?>').'<?php return ob_get_clean();\')."';
}

function 
phptpl_iif($condition$true)
{
    
$args func_get_args();
    for(
$i=1$c=count($args); $i<$c$i+=2)
        if(
$args[$i-1]) return $args[$i];
    return (isset(
$args[$i-1]) ? $args[$i-1] : '');
}
?>
Any help will be greatly appreciated.

Thanks,
jnd52
I fixed it. Sigh. Blush

Apparently, I had to set

Board Settings->
Server and Optimization Options ->
Output template start/end comments?
to yes.

Working like a charm again. Woot!

www.maroonlife.com
[Image: logo4.jpg]
Find all posts by this user
Quote this message in a reply
06-29-2009, 04:08 PM
Post: #42
RE: PHP in Templates and Template Conditionals
Nice Plugin,

Thanks for Release...

vBulletin/Wordpress/Magento/MyBB
Developer and Designer
Visit this user's website Find all posts by this user
Quote this message in a reply
07-21-2009, 04:01 AM (This post was last modified: 07-21-2009 09:58 PM by Steve Moore.)
Post: #43
RE: PHP in Templates and Template Conditionals
EDIT: Figured it out.

Official Site: http://mythotical.com
Coming Soon: http://gifted-angels.com

Follow me on twitter: @stevemoore28
Visit this user's website Find all posts by this user
Quote this message in a reply
07-30-2009, 10:18 AM
Post: #44
RE: PHP in Templates and Template Conditionals
hey this does not seem to work for me,
when i activate the plugin,
it breaks my current theme "Bluefusion"
Any help?

Thanks in Advance
Find all posts by this user
Quote this message in a reply
07-31-2009, 05:39 PM
Post: #45
RE: PHP in Templates and Template Conditionals
Thank you for this plugin!!! Solve my problem took me 2 whole days!~!! Can I donate to you?
Find all posts by this user
Quote this message in a reply
08-01-2009, 10:26 PM
Post: #46
RE: PHP in Templates and Template Conditionals
Ugh... I don't have a clue what I'm doing...
I got the plugin installed and activated.

What I want to do:
Disable the Member List and Calendar links unless the user is logged in; guests should not see these links.

I realize I need to edit the 'header' template, as I see the links there. I just don't know what code to insert to check if a user is logged in or not...

Thanks,
Kerry Wano

"True friends stab you in the front." - Oscar Wilde
Find all posts by this user
Quote this message in a reply
08-01-2009, 10:32 PM
Post: #47
RE: PHP in Templates and Template Conditionals
PHP Code:
<?php if(!empty($mybb->user['uid'])){
echo 
"Your links here";
}
?>

Should work...

Visit this user's website Find all posts by this user
Quote this message in a reply
08-01-2009, 11:12 PM (This post was last modified: 08-01-2009 11:20 PM by kerrywano.)
Post: #48
RE: PHP in Templates and Template Conditionals
Parse error: syntax error, unexpected '{', expecting ',' or ';' in /home/content/62/4727962/html/forums/global.php(438) : eval()'d code(13) : eval()'d code on line 2

This is the error I see when I view the page.
The links are not shown, but something about the error is also preventing the login function from working.

Thanks,
Kerry Wano

"True friends stab you in the front." - Oscar Wilde

EDIT: I figured it out. I was using php, and not the plugin.

My code that works:
Code:
<if $mybb->user['uid'] > 0 then>
(Member List link)
(Calendar link)
</if>
Find all posts by this user
Quote this message in a reply
08-01-2009, 11:40 PM
Post: #49
RE: PHP in Templates and Template Conditionals
oh yes, sorry. I didn't read exactly how this plugin turns it.
Glad you figured it out Smile

Visit this user's website Find all posts by this user
Quote this message in a reply
08-12-2009, 07:19 PM
Post: #50
RE: PHP in Templates and Template Conditionals
Thanks,

Sleepy
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 2 Guest(s)

Contact Us | MyBB | Return to Top | Return to Content | Lite (Archive) Mode | RSS Syndication