0

im new in the forum and im spanish... sorry for my low level of english...

Im using Codeigniter with twig engine in my project but I having some problems...

When I use:

<link rel="stylesheet" href="{{ asset('assets/css/bootstrap.min.css') }}">

Codeigniter throws an exception. It says:

The function "asset" does not exist. Did you mean "assert" in "index.html.twig" at line 12 --- #0 /volume1/web/ci/application/libraries/Twig/ExpressionParser.php(351): Twig_ExpressionParser->getFunctionNodeClass('asset', 12) #1 /volume1/web/ci/application/libraries/Twig/ExpressionParser.php(144): Twig_ExpressionParser->getFunctionNode('asset', 12) #2 /volume1/web/ci/application/libraries/Twig/ExpressionParser.php(84): Twig_ExpressionParser->parsePrimaryExpression() #3 /volume1/web/ci/application/libraries/Twig/ExpressionParser.php(41): Twig_ExpressionParser->getPrimary() #4 /volume1/web/ci/application/libraries/Twig/Parser.php(141): Twig_ExpressionParser->parseExpression() #5 /volume1/web/ci/application/libraries/Twig/TokenParser/Block.php(45): Twig_Parser->subparse(Array, true) #6 /volume1/web/ci/application/libraries/Twig/Parser.php(187): Twig_TokenParser_Block->parse(Object(Twig_Token)) #7 /volume1/web/ci/application/libraries/Twig/Parser.php(95): Twig_Parser->subparse(NULL, false) #8 /volume1/web/ci/application/libraries/Twig/Environment.php(543): Twig_Parser->parse(Object(Twig_TokenStream)) #9 /volume1/web/ci/application/libraries/Twig/Environment.php(595): Twig_Environment->parse(Object(Twig_TokenStream)) #10 /volume1/web/ci/application/libraries/Twig/Environment.php(335): Twig_Environment->compileSource('...', 'index.html.twig') #11 /volume1/web/ci/application/libraries/Twig.php(90): Twig_Environment->loadTemplate('index.html.twig') #12 /volume1/web/ci/application/controllers/welcome.php(15): Twig->display('index.html.twig', Array) #13 [internal function]: Welcome->index() #14 /volume1/web/ci/system/core/CodeIgniter.php(360): call_user_func_array(Array, Array) #15 /volume1/web/ci/index.php(202): require_once('/volume1/web/ci...') #16 {main}

Also throws an exception for any function twig

What is the problem?

Thank you!

Edit

Ok guys, I haven't been unable to resolve my problem, the function doesn't exists but I have got to include my css and js files and also using twig together. The problem was on my .htaccess file (the file is on root directory in my server). The file had written:

RewriteEngine on
RewriteCond $1 !^(index.php|css|js|images|robots.txt) 
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

So I created another .htaccess file on "/ci/assets/.htaccess" with this code:

RewriteEngine off
RewriteCond $1 !^(index.php|css|js|images|robots.txt) 
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

I don't know if this is the best way but this is valid for me and I can work fine.

Any sugestion?

3 Answers 3

0

You need helper file that contents asset() function. But maybe you have a typo:

<link rel="stylesheet" href="{{ assert('assets/css/bootstrap.min.css') }}">
Sign up to request clarification or add additional context in comments.

Comments

0

asset() takes part of the Symfony2 project, not the Twig one.

You should create this function in codeigniter by yourself first.

1) In your custom Twig library, add the following line:

$this->_twig_env->addFunction(
    new Twig_SimpleFunction('asset', 'asset', 
        array('is_safe' => array('html')))
);

2) In your helpers, add the following function (replace assets/ by the path to your assets, here I assume your images, js, ... are available in assets/images, assets/js, assets/...).

if (!function_exists('asset'))
{

   function asset()
   {
      return base_url() . 'assets/';
   }

}

Comments

0

You Might want to add a filter like eg usage is as below

Link rel="stylesheet" href="{{ 'assets/css/bootstrap.min.css' | asset }}">

to achieve above you might want to extend twig filters class and add your filter.

requrie_once("path of Twig_Extension class); // update this 
use Twig_Extension;
use Twig_Filter_Method;

class customExtension  extends Twig_Extension
{

    /**
     * Returns the name of the extension.
     *
     * @return string The extension name
     */
    function getName()
    {
       return "customExtension";
    }

    public function getFilters()
    {
        return array(
           'asset'      => new Twig_Filter_Method($this,'twig_asset_filter'),
        );
    }

    /**
     * @param $path
     *
     * @return string
     */
    function twig_asset_filter($path)
    {
        return 'assets/'.$path;
    }

after which you can use it in same way - this extension is already written and is accessible via composer..

check this link https://github.com/iddigitalagency/Codeigniter-twig-twigextensions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.