symfony2 assetic include assets from themes - php

I have a question about symfony2 assetic. The next situation in project:
We got a themes of project, themes must be stored in one folder. Now it's in:
/
/src
/Dwd
/FrontendBundle
/Resources
/themes
/default
/css
reset.css
style.css
/img
img.png
layout.html.twig
/not_default
/css
reset.css
style.css
/img
img.png
layout.html.twig
and i have the problem of including the styles and images from styles (cssrewrite) filter from theme
In layout.html.twig the next code:
<html>
<head>
{% stylesheets 'bundles/dwdfrontendbundle/css/*' filter="cssrewrite" %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
</head>
<body>
</body>
</html>
And when I do "php app/console assets:install" assetic don't install styles from Dwd/FrontendBundle/Resources/themes...
So my question is How i can add this path or include this css files in layout.html.twig of any file?
p.s. We extend this layout in /views templates trough adding include path to twig.loader service, like {% extends 'layout.html.twig' %}.

Related

Including Assets (CSS, JS) in Symfony 4 / Twig Template

Im learning Symfony and trying to set up a boilerplate app in Symfony 4
This Symfony document describes how to include assets in your page, namely using the asset package like so..
<img src="{{ asset('images/logo.png') }}" alt="Symfony!" />
<link href="{{ asset('css/blog.css') }}" rel="stylesheet" />
I have installed this package and trying to link to a css file in my /public directory.
I know Symfony recommends placing assets in an /assets folder at the root, but I would like to avoid this if possible. It makes more sense to me to place assets in the public directory.
My Twig base template is as follows:
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
{% block stylesheets %}
<link href="{{ asset('public/css/main.css') }}" rel="stylesheet" />
{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
<footer>footer</footer>
</html>
Problem
When I load the route/page/template, the linked css is rendered as a simple link from the root (as if Im linking it as <link href="public/css/main.css" rel="stylesheet" /> - there is of course no route set for public/* and this returns 404/Not Found.
Its as if the asset() function is not even used.
Ive tried
Restarting my local Symfony server
moving the asset to an assets folder at the root at adjusting the link accordingly
verifying that the asset package was installed (it is)
googling this issue (nothing for Symfony 4)
Questions
How can I let Symfony know that the public/* path is a filesystem path to assets, NOT a URL route, and include my assets successfully?
Is there such a feature to set a default location for assets other than the recommended /assets folder at the root?
Paths in the asset function should be relative to your public directory, not the project root.
So, pretending your server's docroot is public_html, and an asset is in public_html/css/main.css, the asset call would be {{ asset('css/main.css') }}.
Additional note: The asset function doesn't give you the ability to put assets outside the public directory. They must be in the public directory.
Firstly maybe you have to add the asset functionality to Symfony:
composer require symfony/asset
Then you should to use in Twig temlate the relative path to your public directory:
<link href="{{ asset('css/style.css') }}" rel="stylesheet" />
for the public/css/style.css style file.
BTW it is also works for Symfony 5.
Check if webpack is watching files ... (yarn watch) After hours of searching for an answer one of my project team members asked if I had that running ={ Worked immediately after that.

Symfony the right way assets path

I am trying to find out, how to get path of my css and js files.
This is my base.html.twig in app/Resources/views/base.html.twig
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>{% block title %}Welcome!{% endblock %}</title>
<link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css">
</head>
<body>
{% block body %}{% endblock %}
<script src="js/jquery-2.1.4.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
It cant find my assets, because it is seeking in web/ directory by default. How should my path look like to find the files in app/Resources/views?
thanks!
Try to include stylesheet as below :
{% stylesheets 'css/bootstrap.min.css'
'css/bootstrap-theme.min.css'
filter='cssrewrite' %}
{% enstylesheets %}
Also you should install assets and you can use symlink option to assets:install command. This will make symlink in web folder to your bundle public folders
app/console assets:install --symlink
Without symlink option this task makes copy of files, so your changes doesn't affect.
You should also use assetic:dump command
app/console assetic:dump
Clear the cache as well.

Symfony2 Twig Style Sheet

I am following a tutorial for symphony 2 but cant get the style sheet to load all I get is a 404.
I have tried also adding it into the route but I get a permission error on the style sheet.
app/Resources/views/base.html.twig
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset=utf-8" />
<title>{% block title %}symblog{% endblock %} - symblog</title>
{% block stylesheets %}
<link href="{{ asset('css/screen.css') }}" type="text/css" rel="stylesheet" />
{% endblock %}
I have added the style sheet into app/Resources/views/web/css/screen.css
Any ideas why this is not working? Tutorial
You put css files in a wrong directory.
It should be web/css/screen.css instead of app/Resources/views/web/css/screen.css
I also suggest you to use assetic instead of manually put css and js to the web directory. These articles might help (take a look at the assets:install command):
How to Use Assetic for Asset Management
Linking to Assets
Including Stylesheets and JavaScripts in Twig
Projects assets (resources from your app/ folder) are ignored by assets:install command. Only resources from bundles will be copied into the web/ folder automatically.
You should place your app (project) resources directly in the web/ folder.
http://symfony.com/doc/current/cookbook/assetic/asset_management.html
By the way: ONLY the web/ folder is public.
Look at the rendered source code of your html in the browser and you will see that your css file is prefixed with a web/. This is what the asset() command does

symfony2 - the assetic assetic:dump command doesn't create stylesheets correctly

For some reason I can't load the css files.
Here is the structure of the bundle that I use
BD
WebsiteBundle
public
css
And here is how I try to load the css files
{% stylesheets 'bundles/bdwebsite/css/*' filter='cssrewrite' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
I did assetic:dump
What I'm doing wrong?
You referred to bundles/bdwebsite/css/* in your stylesheets tag.
This way assetic looks for all css files in web/bundles/bdwebsite/css (a folder that currently does not exist) and not in src/BD/WebsiteBundle/public/css.
In order to have your files in the right place before executing assetic:dump...
... use app/console assets:install web which will copy them to the web folder...
... or my recommendation app/console assets:install web --symlink for symlinks.

Symfony 2.1 - problems loading assets and an exception thrown by assetic

This is a bit frustrating. According to Symfony best practices, a bundle's web assets (images, css, js) should be placed in src/vendor/path/to/bundle/Resources/public. Running app/console assets:install copies the contents of that public folder to web/bundles/bundlename. In the official documentation, Twig templates are shown to grab these assets with code like:
{% block stylesheets %}
<link rel="stylesheet" type="text/css" href="{{ asset('/css/styles.css') }}
{% endblock %}
When I try it, instead of magically grabbing my assets from web/bundles/bundlename/css/styles.css, it instead just goes for web/css/styles.css. Is this expected behavior? The official documentation is less than clear about this.
In order to try to combat this issue, I tried embracing assetic's ability to dynamically serve assets. I tried:
{% stylesheets '#mybundle/Resources/public/css/*' %}
<link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
{% endstylesheets %}
But got the following exception:
An exception has been thrown during the compilation of a template ("You must add mybundle to the assetic.bundle config to use the {% stylesheets %} tag in mybundle:Home:index.html.twig.") in "/home/kevin/www/src/mybundle/Resources/views/Home/index.html.twig
The official Symfony documentation makes it appear that it should work out of the box with no configuration necessary.
So, TLDR:
Am I not understanding how assets should be loaded?
How do I address the assetic exception?
To fix the assetic exception you need to configure your bundle in your config.yml like this:
assetic:
bundles: [ MyAwesomeBundle ]
The {{ asset(...) }} twig function will serve files relative from your web root.
You use app/console assets:install to install static assets within your web root and you can later easily point to them using the asset function like this {{ asset('/mybundle/css/site.css') }}

Categories