How to add a require_once('file.php') statement in twig templates?

JhovaniC picture JhovaniC · Apr 11, 2012 · Viewed 8.3k times · Source

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();

Answer

JhovaniC picture JhovaniC · Apr 11, 2012

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