PHP未捕获异常:'Twig_Error_Syntax'消息:'未知'资产“功能”。

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.

php

1个回答



您可以在教程中找到的 asset </ code>函数是Symfony添加的函数,不是内置于twig本身。< / p>

您可以在文档中看到twig自己的功能</ 一>。 (链接用于twig2,不确定你使用的是什么。)</ p>
</ div>

展开原文

原文

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.)

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问