-1

Hi I have a following Helper created in my bundle:

Edu/AccountBundle/Service/Helper.php

namespace Edu\AccountBundle\Service;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Form\Form;
use Edu\AccountBundle\CommonFunctions\CommonFunctions;

class Helper{

private $session;
private $router;
private $documentManager;
private $commonFunctions;

/*
 * Class:Constructor
 * @DESC:its a very first method which always called whenever this call going to be instantiate
 * @param : @session,@route,@db
 * @sunilrawat@indivar.com
 * @03-06-2016
 */
public function __construct(Session $session, Router $router, DocumentManager $dm)
{
    $this->session = $session;
    $this->router = $router;
    $this->dm = $dm;
    $this->commonFunctions = new CommonFunctions();
}

/*
 * Class:checkFinancialYearLockPeriod
 * @DESC:check a particular date on which accounts Financial Year data will be Locked So that user can not make any modifications after that
 * @param : @db
 * @sunilrawat@indivar.com
 * @03-06-2016
 */
public function checkFinancialYearLockPeriod($dm){
    $currentYearIs = $this->commonFunctions()->checkFinancialYear($this->dm);
    $exploadsCurrentYear = explode("-",$currentYearIs);
    $currentFinancialYearTo = "31-03-".$exploadsCurrentYear[1];
    return $effectiveDate = date('d-m-Y', strtotime("+63 days", strtotime($currentFinancialYearTo)));        
}
}

 //My services.xml code is as following:
<service id="edu.account.helper" class="Edu\AccountBundle\Service\Helper">
            <argument type="service" id="session"/>
            <argument type="service" id="router"/>
            <argument type="service" id="doctrine_mongodb.odm.document_manager"/>
            <tag name="twig.helper" />
        </service>     
 //So now I want to call it on Twig directly. So I am calking it as   follows:
 {{ edu.account.helper.checkFinancialYearLockPeriod() }}

But its not working as throwing following error: "Variable "edu" does not exist".

Please help what is missing in this, Thanks in advance:

Sunil Rawat
  • 709
  • 10
  • 41
  • Yes, in my case this solution not working.. can you please assist if possible – Sunil Rawat Jun 03 '16 at 05:13
  • You should add service with globals config settings to twig section in config.yml. And then call it by name from globals, not with service id – Denis Alimov Jun 03 '16 at 05:15
  • but it is also registered properly. I have checked to confirm it by running following command: php app/console container:debug edu.account.listener its showing that is it registered. – Sunil Rawat Jun 03 '16 at 05:19
  • C'mon man. Please read carefully answer to that question and what I am writing to you. Add service to twig section to globals settings in config.yml. call it from twig template by name that was given by you in that section. – Denis Alimov Jun 03 '16 at 05:22
  • twig: debug: %kernel.debug% strict_variables: %kernel.debug% globals: edu.account.helper: %edu.account.helper% Still error: You have requested a non-existent parameter "edu.account.helper". – Sunil Rawat Jun 03 '16 at 05:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113699/discussion-between-user3458514-and-denis-alimov). – Sunil Rawat Jun 03 '16 at 05:41

1 Answers1

0

Helper in Symfony2 not able to call on Twig is totally right.

There is no tag called "twig.helper". Only one called "twig.extension". If you want to use that, you should extend \Twig_Extension. See http://symfony.com/doc/current/cookbook/templating/twig_extension.html .

But in your case, you need to do the following:

#app/config/config.yml
twig:
    globals:
        edu_account_helper: "@edu.account.helper" #key is name in twig, value is reference to your service

This means in Twig-Scope, it is called "edu_account_helper". Snake-cased.

{{ edu_account_helper.checkFinancialYearLockPeriod() }}
Community
  • 1
  • 1
Fivetide
  • 161
  • 5