简体   繁体   中英

PHP Uncaught exception: 'Twig_Error_Syntax' Message: 'Unknown “asset” function.'

I'm using MVC in php and i'm trying to use twig as templating engine. I'm trying to use asset for including css. But i'm facing some errors. It doesnot recognize assest function.

I have used following code:

My core base:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">

    <title>{% block title %}{% endblock %} </title>
    <!-- <link href="../../vendor/style.css" rel="stylesheet"> -->

       <link href="{{ asset('../../vendor/style.css') }}" type="text/css" rel="stylesheet" />

</head>
<body>
  <nav>
    <a href="/">Home</a>
    <a href="/posts/index">Posts</a>
  </nav>
  {% block body %}
  {% endblock %}
</body>
</html>

Following is the view that extends the base template, dashboard:

{% extends "base.html" %}

{% block title %}Dashboard{% endblock %}

{% block body %}

    <h1>Welcome User!</h1>
    <a href="logout"><button>Logout</button></a>

{% endblock %}

But i'm getting following error:

Uncaught exception: 'Twig_Error_Syntax'

Message: 'Unknown "asset" function.'

Full stack trace of the error is below: Stack trace:

#0 C:\xampp\app\mvc\vendor\twig\twig\lib\Twig\ExpressionParser.php(364): Twig_ExpressionParser->getFunctionNodeClass('asset', 9)
#1 C:\xampp\app\mvc\vendor\twig\twig\lib\Twig\ExpressionParser.php(152): Twig_ExpressionParser->getFunctionNode('asset', 9)
#2 C:\xampp\app\mvc\vendor\twig\twig\lib\Twig\ExpressionParser.php(92): Twig_ExpressionParser->parsePrimaryExpression()
#3 C:\xampp\app\mvc\vendor\twig\twig\lib\Twig\ExpressionParser.php(45): Twig_ExpressionParser->getPrimary()
#4 C:\xampp\app\mvc\vendor\twig\twig\lib\Twig\Parser.php(123): Twig_ExpressionParser->parseExpression()
#5 C:\xampp\app\mvc\vendor\twig\twig\lib\Twig\Parser.php(79): Twig_Parser->subparse(NULL, false)
#6 C:\xampp\app\mvc\vendor\twig\twig\lib\Twig\Environment.php(512): Twig_Parser->parse(Object(Twig_TokenStream))
#7 C:\xampp\app\mvc\vendor\twig\twig\lib\Twig\Environment.php(544): Twig_Environment->parse(Object(Twig_TokenStream))
#8 C:\xampp\app\mvc\vendor\twig\twig\lib\Twig\Environment.php(363): Twig_Environment->compileSource(Object(Twig_Source))
#9 C:\xampp\app\mvc\vendor\twig\twig\lib\Twig\Template.php(329): Twig_Environment->loadTemplate('base.html', NULL)
#10 C:\xampp\app\mvc\vendor\twig\twig\lib\Twig\Environment.php(373) : eval()'d code(11): Twig_Template->loadTemplate('base.html', 'User/dashboard....', 1)
#11 C:\xampp\app\mvc\vendor\twig\twig\lib\Twig\Environment.php(385): __TwigTemplate_6a4c151f517970c46aab3eedfdfea56b57d9fb29e4119bece3b41b9de1f71014->__construct(Object(Twig_Environment))
#12 C:\xampp\app\mvc\vendor\twig\twig\lib\Twig\Environment.php(288): Twig_Environment->loadTemplate('User/dashboard....')
#13 C:\xampp\app\mvc\Core\View.php(35): Twig_Environment->render('User/dashboard....', Array)
#14 C:\xampp\app\mvc\App\Controllers\User.php(22): Core\View::renderTemplate('User/dashboard....')
#15 [internal function]: App\Controllers\User->loginAction()
#16 C:\xampp\app\mvc\Core\Controller.php(25): call_user_func_array(Array, Array)
#17 C:\xampp\app\mvc\Core\Router.php(100): Core\Controller->__call('login', Array)
#18 C:\xampp\app\mvc\public\index.php(57): Core\Router->dispatch('user/login'

I don't know what to do now. Can anyone help me? How can i use assest or are there any alternative wy to include my css and js.

The asset function you may find in tutorials is one added by Symfony, not built into twig itself.

You can see twig's own function in its documentation . (Link is for twig2, not sure what you're using.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM