Hello, I followed docs and I got an error Unknown "asset" function.
This is my tempate:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
<link rel="stylesheet" href="{{ asset('build/global.css') }}">
{% block stylesheets %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
<script src="{{ asset('build/app.js') }}"></script>
{% block javascripts %}{% endblock %}
</body>
</html>
My composer.json
{
"name": "symfony/skeleton",
"type": "project",
"license": "proprietary",
"description": "Project description",
"require": {
"php": "^7.1.3",
"sensio/framework-extra-bundle": "^4.0",
"symfony/console": "^3.3",
"symfony/flex": "^1.0",
"symfony/framework-bundle": "^3.3",
"symfony/translation": "^3.3",
"symfony/twig-bundle": "^3.3",
"symfony/web-profiler-bundle": "^3.3",
"symfony/web-server-bundle": "^3.3",
"symfony/webpack-encore-pack": "^1.0",
"symfony/yaml": "^3.3",
"twig/extensions": "^1.5",
"twig/twig": "^2.0"
},
"require-dev": {
"codeception/codeception": "^2.3",
"symfony/dotenv": "^3.3"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true,
"platform": {
"php": "7.2",
"ext-mbstring": "1.0",
"ext-zip": "1.0"
}
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"scripts": {
"auto-scripts": {
"make cache-warmup": "script",
"assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*",
"symfony/twig-bundle": "<3.3",
"symfony/debug": "<3.3"
},
"extra": {
"symfony": {
"id": "01BS486MD5XBCQR9X1ATS0099K",
"allow-contrib": false
}
}
}
I removed vendor and did composer install again but without change. And here's the link to documentation https://symfony.com/doc/current/frontend/encore/simple-example.html
And stack trace:
Twig_Error_Syntax:
Unknown "asset" function.
at templates/base.html.twig:6
at Twig_ExpressionParser->getFunctionNodeClass('asset', 6)
(vendor/twig/twig/lib/Twig/ExpressionParser.php:364)
at Twig_ExpressionParser->getFunctionNode('asset', 6)
(vendor/twig/twig/lib/Twig/ExpressionParser.php:152)
at Twig_ExpressionParser->parsePrimaryExpression()
(vendor/twig/twig/lib/Twig/ExpressionParser.php:92)
at Twig_ExpressionParser->getPrimary()
(vendor/twig/twig/lib/Twig/ExpressionParser.php:45)
at Twig_ExpressionParser->parseExpression()
(vendor/twig/twig/lib/Twig/Parser.php:123)
at Twig_Parser->subparse(null, false)
(vendor/twig/twig/lib/Twig/Parser.php:79)
at Twig_Parser->parse(object(Twig_TokenStream))
(vendor/twig/twig/lib/Twig/Environment.php:529)
at Twig_Environment->parse(object(Twig_TokenStream))
(vendor/twig/twig/lib/Twig/Environment.php:561)
at Twig_Environment->compileSource(object(Twig_Source))
(vendor/twig/twig/lib/Twig/Environment.php:368)
at Twig_Environment->loadTemplate('base.html.twig', null)
(vendor/twig/twig/lib/Twig/Template.php:329)
at Twig_Template->loadTemplate('base.html.twig', 'home.twig', 1)
(var/cache/dev/twig/0f/0fea66afae7ab11ecb2dff0540c5c47f2945baed7ceee74c72ed4181f04b87cd.php:11)
at __TwigTemplate_9c79120184851d817bc7e14e31ec694b0137744535017ebd5c6fb3484bafcaa4->__construct(object(Twig_Environment))
(vendor/twig/twig/lib/Twig/Environment.php:397)
at Twig_Environment->loadTemplate('home.twig')
(vendor/twig/twig/lib/Twig/Environment.php:289)
at Twig_Environment->render('home.twig', array('title' => 'Hello', 'body' => 'adasd'))
(vendor/symfony/framework-bundle/Controller/ControllerTrait.php:245)
at Symfony\Bundle\FrameworkBundle\Controller\Controller->render('home.twig', array('title' => 'Hello', 'body' => 'adasd'))
(src/Controller/HomeController.php:15)
at App\Controller\HomeController->__invoke()
at call_user_func_array(object(HomeController), array())
(vendor/symfony/http-kernel/HttpKernel.php:153)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor/symfony/http-kernel/HttpKernel.php:68)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor/symfony/http-kernel/Kernel.php:171)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(public/index.php:28)
Hi @VeeeneX,
This issue is more related to Symfony than Encore, but did you enable the Asset component in your app/config/config.yml file?
framework:
# (...)
assets: ~
@Lyrkan Thanks I enabled it but now I got another Asset support cannot be enabled as the Asset component is not installed.
Ok, resolved it's simple just composer req asset
As Symfony 4 and Flex approach, we鈥檒l need to add this composer require line to the docs.
@weaverryan please do, I was just following tutorials and right now https://symfony.com/doc/current/frontend/encore/simple-example.html still leads straight towards this error ;)
@weaverryan up! The same as @curry684 right now 馃槃
Yeah I had the same issue, thanks to this thread :) I resolve it with this command:
composer req asset
I鈥檓 reopening: this is a legit docs issue, or maybe a recipe issue (we might require the asset component in the encore pack) or just have the user require both.
Reopening one last time. We greatly improved the error message when missing the component, but I believe we still need to update the docs to show asset being required
I think information about this should be put here: https://symfony.com/doc/current/frontend/encore/simple-example.html
Just before the HTML block that shows asset(), it would be the first time a user encounters this issue when following the docs in order. I tried to do this in a PR however I am not sure of the best way to display it. I thought it might be useful in a block with the ! icon but the Markdown comes out a bit strange and I'm not sure how it would look on the docs, maybe the wording needs rephrasing for the last sentence so it flows nicely.
.. tip::
To use this function, install the *asset* package:
.. code-block:: terminal
$ composer require asset
You can now use the ``asset()`` function:
(mostly taken from: https://github.com/symfony/symfony-docs/pull/8804)
This should be better now. The official install docs use composer require encore, which grabs the WebpackEncoreBundle, which has asset as a dependency.
Most helpful comment
Ok, resolved it's simple just
composer req asset