2

I'm new to this symfony2 and twig so I'm not very familiar with it. I need to add a require_once(file.php) into a mytemplate.html.twig but I just don't know how to do that!! :S
The thing is that I have to add a social slider to my website which is being developed with symfony2.
The plugin's requirement is to add the require_once(file.php) statement so any ideas on how to do that ? :S This is the file.php

require_once(dirname(__FILE__) . '/common.inc.php');


DEFINE('FBLB_DEMO',true);

function fblb_slider()
{
//print_r($_REQUEST);
global $fblb_preview_options;
if (isset($_REQUEST['Preset']) && FBLB_DEMO===true && FBLB_CONFIG===0)
{
    require(dirname(__FILE__) . '/fblbconfig.inc.php');
    require_once(dirname(__FILE__) . '/config.php');
    $fblb_preview_options = array_merge((array)$fblb_options,    (array)$FBLB_Presets[$_GET['Preset']]['Options']);
}
if (isset($_REQUEST['preview']) && (FBLB_CONFIG===1 || FBLB_DEMO===true))
{
    $options = $fblb_preview_options;
}
else
{
    require_once(dirname(__FILE__) . '/config.php');
    $options = $fblb_options;
    if($options['DisableByGetParamN'] && $options['DisableByGetParamV'] && $_GET[$options['DisableByGetParamN']]==$options['DisableByGetParamV'])
    {
        return;
    }
}
if ($options['Enable'] == 1 && $options['FacebookPageURL'])
{
    require_once(dirname(__FILE__) . '/fblb_slider.php');
}
if ($options['TW_Enable'] == 1 && $options['TW_Username'])
{
    require_once(dirname(__FILE__) . '/fblb_tw_slider.php');
}
if ($options['GP_Enable'] == 1 && $options['GP_PageID'])
{
    require_once(dirname(__FILE__) . '/fblb_gp_slider.php');
}
if ($options['YT_Enable'] == 1 && $options['YT_Channel'])
{
    require_once(dirname(__FILE__) . '/fblb_yt_slider.php');
}
if ($options['LI_Enable'])
{
    require_once(dirname(__FILE__) . '/fblb_li_slider.php');
}
}
fblb_slider();
JhovaniC
  • 304
  • 3
  • 17
  • Well what's the code for the slider? Is it a complex script or is it just html with few php variables? – prodigitalson Apr 11 '12 at 03:02
  • The slider provides a lot of php files but I just need to add the require_once for a single php file which will add the slider to the website – JhovaniC Apr 11 '12 at 04:00
  • BTW the script that I'm supposed to require has a lot of requires and some functions – JhovaniC Apr 11 '12 at 04:14

2 Answers2

5

Ok guys I solved it !!! First I had to create the twig extension >_>
I follow this guide http://www.solidwebcode.com/web-development/twig-extensions-symfony-2/ or this one in spanish http://facultia.com/blog/2011/08/08/extensiones-personalizadas-twig-proyectos-symfony2/
I used

Twig_Function_Method($this, 'your_method_name')  

Instead of filter and it worked well !! After that I just gave execution permission to the script and that was it =D !! In the twig template you use your method like this

{{ your_method_name(your_params) }}

Thanks to everyone that tried to help !!! =D

JhovaniC
  • 304
  • 3
  • 17
  • 1
    Can you give an example code? I need the same thing - call PHP's require_once inside twig template and I can't figure out how to implement this – nKognito Jun 16 '12 at 11:11
  • well what you need is a twig extension, try the guides and if you can't I'll post the code here. – JhovaniC Jun 17 '12 at 16:38
1

Short answer: You can't include PHP code from inside a twig template.

I didn't look at your posted code in any real detail but you will probably want to use an embedded controller to wrap your slider.

Read through this:

http://symfony.com/doc/current/book/templating.html

Especially the section on embedded controllers. It will be up to your embedded controller to include the myfile.php file, capture the slider's generated html/js code (which is what I assume it does) and then return it.

Cerad
  • 48,157
  • 8
  • 90
  • 92
  • Thanks, but instead I used a twig extension. The thing is that the oficial documentation about it, isn't detailed so I had to look for an outside guide xD But everything's running well! – JhovaniC Apr 11 '12 at 21:10