Jump to content


- - - - -

Including a php file in a tpl file


9 replies to this topic

#1 flyingtrapeze

    Junior Member

  • Members
  • PipPip
  • 18 posts

Posted 14 August 2009 - 05:05 AM

Hi there

I was wondering how I can include a php file in a tpl file.

The php file displays a few things from another database and is totally independant of Censura.
Php code doesn't seem to work in the templates (tpl files.) Even things like
<?php echo 'a'; ?> won't work.

How can I include php code or include my php file in a tpl file?

Thanks

#2 Gavin

    Censura Product Manager

  • Administrators
  • 417 posts

Posted 17 August 2009 - 02:10 PM

Hi,
The template uses smarty syntax, so you can't use php code directly in the normal way. You can use php however with the {php}{/php} tags: http://www.smarty.ne...unction.php.php. However this is not ideal, as the purpose of a template is to remove "business" logic like accessing the database from the "display" logic, which makes changes to the display much easier.
I would recommend that you create a plugin, which isn't as hard as it sounds... create a file in system/functions/smartyFunctions named function.my_function.php, replace my_function with an appropriate name for what you are trying to do. Inside the file define a function:
function smarty_function_my_function($params, $smarty) {
  return "hello world";
}
Then in your template use:
{my_function param1="value1" param2="value2"}
...which will output "hello world" in your template in this instance. The function you just defined will be executed, with the parameters param1 and param2 passed in the $params associative array. The return value of the function is then output by the template.
Please note that the function should return a value, not echo a value. If your include outputs data itself, you could use output buffering:
function smarty_function_my_function($params, $smarty) {
  ob_start(); //start the output buffering
  include "my_include.php"; //do your include
  $data = ob_get_contents(); //the output of the include is now stored in $data
  ob_end_clean(); //stop output buffering
  return $data; //return the output of the include
}
Hope this helps, let me know if you have any more questions. For more about smarty plugins, see the documentation: http://www.smarty.ne...s.functions.php
Best Regards
Gavin

Edited by Gavin, 17 August 2009 - 02:16 PM.

Gavin Vincent, Censura Lead Developer
www.gavinvincent.co.uk

#3 flyingtrapeze

    Junior Member

  • Members
  • PipPip
  • 18 posts

Posted 18 August 2009 - 01:39 AM

Hi Gavin
Thanks for the very clear and easy to follow explanation. I created a plugin and it works perfectly.
:)

#4 stormrider

    Senior Member

  • Customers
  • 245 posts

Posted 18 August 2009 - 09:04 AM

flyingtrapeze said:

Hi Gavin
Thanks for the very clear and easy to follow explanation. I created a plugin and it works perfectly.
:)

What does your plugin do? Plugin development and sharing is an area that could use expansion here.

#5 Gavin

    Censura Product Manager

  • Administrators
  • 417 posts

Posted 18 August 2009 - 10:30 AM

When I say plugin, I mean it's a smarty plugin, not a censura plugin. Smarty plugins allow you to execute custom php code from within the template (usually something that's doesn't use any censura code). Censura plugins (allowing you to use censura code and access the data) are still something that's in the planning stage.

Edited by Gavin, 18 August 2009 - 10:38 AM.

Gavin Vincent, Censura Lead Developer
www.gavinvincent.co.uk

#6 flyingtrapeze

    Junior Member

  • Members
  • PipPip
  • 18 posts

Posted 18 August 2009 - 11:00 PM

Hi Stormrider

I made a very simple plugin which was pretty much a duplicate of Gavin's code -

function smarty_function_my_function($params, $smarty) {

  ob_start(); //start the output buffering

  include "my_include.php"; //do your include

  $data = ob_get_contents(); //the output of the include is now stored in $data

  ob_end_clean(); //stop output buffering

  return $data; //return the output of the include

} 

Except I changed the my_include.php file to use a custom php page that fetched stuff from a different database and echoed it to the page.

#7 Kristin

    Junior Member

  • Members
  • Pip
  • 2 posts

Posted 24 November 2009 - 08:34 AM

I want to add long script for Webstatistic web counter in html format by using the "include" function in /skins/styleTemplates/global/index.tpl and cart.tpl.

Your include script is useable in php page but in html like this .tpl. Correct me if Im wrong.

Thanks.

#8 Darren

    Administrator

  • Administrators
  • 868 posts
  • LocationUK

Posted 24 November 2009 - 09:07 PM

Kristin said:

I want to add long script for Webstatistic web counter in html format by using the "include" function in /skins/styleTemplates/global/index.tpl and cart.tpl.

Your include script is useable in php page but in html like this .tpl. Correct me if Im wrong.

Thanks.

For including a HTML file, you should be able to include it the same way using the standard SMARTY include function.

#9 Jackson

    Junior Member

  • Members
  • Pip
  • 1 posts

Posted 05 March 2010 - 11:57 AM

Gavin that was so helpful nicely done..........

#10 ihstiv

    Junior Member

  • Members
  • Pip
  • 9 posts

Posted 03 April 2011 - 03:38 AM

Has anyone written plugins that call the vbulletin 4 header and footer code for their forum integration?

If so, would you like to share it?

thanks in advance for any help.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users