1
Catzwolf
Using PHP in smarty templates.. How to do it the easy way.
  • 2010/7/24 20:11

  • Catzwolf

  • Home away from home

  • Posts: 1392

  • Since: 2007/9/30


When Smarty was first introduced into Xoops, I heard many people say they didn't like it because you can't use PHP directly within the templates. While this is true, there are however a few workarounds that will allow you to basically use PHP native function, as is they are smarty variables.

Lets take one example, the use of sprintf('ba'l, _CONSTANT); is something that developers take advantage of everyday in their code, but there has been times where I really wanted to do this in smarty and yes, you can do it without having to code a new plug-in.

Example:
<{'Hey %s, The time is: %s'|sprintf:$name:$time}>


Smarty has many useful built in smarty function as well, and we can combine both PHP and these functions to help keep our presentation smoother or nicer or more logical, which ever way you want to see it.

Another issue that has bugged me for a long time is having to create select menus in PHP and then added them afterwards. This to me is basically doing the work twice. Here is an example of creating a pull-down select menu in smarty.

First the PHP part:

$myMenu = array( => 'Cats'=> 'wolf'=> 'dog'=> 'mice'=> 'men'=> 'women' );
$xoopsTpl->assign'selected');
$xoopsTpl->assign'mymenu'$myMenu );


Smarty:

http://www.smarty.net/manual/en/language.function.html.options.php

<{html_options name=active id=active onchange=document.myform.submit(); options=$mymenu selected=$selected}>


The end result:
<select size="1" name="active" id="active" onchange="document.myform.submit();">
<
option value="0" selected="selected">Catsoption>
<
option value="1">wolfoption>
<
option value="2">dogoption>
<
option value="4">miceoption>
<
option value="5">menoption>
<
option value="6">womenoption>
select>


Now here is an example of this, without using $xoopsTpl->assign();

We all know that sometimes we want to do this, but do not have access to the core files or a smarty plug-in to do it. So why not do the whole load from the smarty template (and at this point I just heard Trabis scream NOOOOOO!! lol).

What we need to do is manipulate the smarty variables to add what we want and then we can access the smart variable like any other one.

First we need to tell smarty that we're playing around with PHP and not smarty, so we use the PHP smarty tag

<{php}><{/php}>


Adding out new PHP code and assigning some smarty templates:
<{php}>
    
$this->_tpl_vars['mymenu'] = array( => 'Cats'=> 'wolf'=> 'dog'=> 'mice'=> 'men'=> 'women' );
    
$this->_tpl_vars['selected'] = 1;
<{/
php}>


And the smarty code:
<{html_options name=active id=active onchange=document.myform.submit(); options=$mymenu selected=$selected}>


The nice and most obvious reason to do this is so we don't have to keep redoing it over and over with PHP. Once the template has been created and cached it never gets done again.

So, now you know, you can use PHP in smarty just like others do in Joomla. Yes you can even include PHP files and work from them....

Have fun...

ATB

Catz



2
trabis
Re: Using PHP in smarty templates.. How to do it the easy way.
  • 2010/7/24 21:07

  • trabis

  • Core Developer

  • Posts: 2269

  • Since: 2006/9/1 1


Thanks again for this tips!

Some thoughts:
$this refers to a xoopsTpl instance so we can use $this->assign() or other xoopsTpl method. There is no need to use $this->_tpl_vars unless you want to read a previously assigned variable.

I would prefer using a file include for that matter, I often use this on a theme:
<{include_php file="file:$xoops_rootpath/themes/$xoops_theme/ex_assign.php"}>


here is an example of an extension file(by Ryuji AMANO):
// $Id$
// FILE        ::    ex_assign.php
// AUTHOR    ::    Ryuji AMANO 
// WEB        ::    Ryu's Planning 
//

global $xoopsUser$xoopsModule;
if (
is_object($xoopsUser)) {
    
$pm_handler =& xoops_gethandler('privmessage');

    
$criteria = new CriteriaCompo(new Criteria('read_msg'0));
    
$criteria->add(new Criteria('to_userid'$xoopsUser->getVar('uid')));
    
$message $pm_handler->getCount($criteria); $message =1;
    if (
$message 0$message "".$message."";
    
$this->assign("ex_new_messages"$message);
}

if ( 
is_object($xoopsModule) ) {
    
$this->assign('ex_moduledir'$xoopsModule->getVar('dirname'));
}


require_once 
XOOPS_ROOT_PATH."/modules/system/blocks/system_blocks.php";
$mainmenu b_system_main_show();
foreach(
$mainmenu["modules"] as $module){
    if (
count($module["sublinks"]) > ){
        
$mainmenu["sublinks"] = $module["sublinks"];
    }
}
$this->assign("ex_mainmenu"$mainmenu);
$this->assign("mymodules"$mainmenu["modules"]);

global 
$xoopsRequestUri;
if ( 
is_object($xoopsModule) ) {
  
$this->assign('ex_moduledir'$xoopsModule->getVar('dirname'));
  
$this->assign('ex_modulename'$xoopsModule->getVar('name'));
  if( 
count($xoopsModule->subLink()) > ) {
    
$replacedUri preg_replace("//modules/".$xoopsModule->getVar('dirname')."/(.*)$/i""$1"$xoopsRequestUri);
    foreach( 
$xoopsModule->subLink() as $sublink ) {
      if( 
$sublink['url'] == $replacedUri ) {
        
$this->assign('ex_sublinkname'$sublink['name']);
      }
    }
  }
}

3
wammes
Re: Using PHP in smarty templates.. How to do it the easy way.
  • 2010/9/6 20:31

  • wammes

  • Not too shy to talk

  • Posts: 101

  • Since: 2002/1/3 1


Hi gentlemen,

this is almost genius! It is almost what I'm looking for.

I have a smarty variable from Extcal
<{$event.formated_event_start}>
It gives the date of a upcoming event in this format: 01/02/2011

I want it to look like 01 feb

Now the code
<{$event.formated_event_start|date_format:"%b"}>
doesn't work. |date_format is making it the current date, not the date in the event.formated_event_start string. I guess because it cannot figure out a timestamp or something.

So I guess I have to make up something like described above. I prefer the include of a php file. But how can I get the value of
<{$event.formated_event_start}>
"known" in the php file?

I'm sorry I'm no coder (as you might guess)..

Thank you very much for any response!

4
ghia
Re: Using PHP in smarty templates.. How to do it the easy way.
  • 2010/9/7 7:52

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


Normally this should work, but you may want " %d %b %y" as format.
Probably the default PHP format can't match the 01/02/2011 and you have to alter some default date format settings for PHP.

Other modifier you could try is replace, but it will become ugly.
<{$event.formated_event_start|replace:'/01/':' jan '|replace:'/02/':' feb '.......|replace:'/12/':' dec '}>


5
wammes
Re: Using PHP in smarty templates.. How to do it the easy way.
  • 2010/9/7 18:50

  • wammes

  • Not too shy to talk

  • Posts: 101

  • Since: 2002/1/3 1


bedankt ghia,

the replace function did it. It is indeed a ugly solution, but I'll never see it :)

Thanks!

EDIT:
Now I fixed the |date_format with the replace function:
<{$event.formated_event_start|replace:'/':'-'|date_format:"%A, %B %e, %Y"}>


I think this is the most elegant of all the ugly solutions :)

6
kaotik
Re: Using PHP in smarty templates.. How to do it the easy way.
  • 2010/10/4 12:24

  • kaotik

  • Just can't stay away

  • Posts: 861

  • Since: 2004/2/19


Thanks Catz
This really is a great way to work with select boxes and dropdowns.


7
Fabian
Re: Using PHP in smarty templates.. How to do it the easy way.
  • 2011/8/24 8:05

  • Fabian

  • Just popping in

  • Posts: 1

  • Since: 2011/8/24


the replace function is an ugly solution though, but sometimes the only one :) if it resolves the issue, why not..

Login

Who's Online

322 user(s) are online (105 user(s) are browsing Support Forums)


Members: 0


Guests: 322


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits