I am new to a Symfony 2 project that contains an SCSS file in the Resources folder: myproject.scss
In the head of the HTML files, the css version of this file gets included:
#MyBundle/Resources/public/css/myproject.css
PHP Storm indicates "Missing asset". The HTML cannot be rendered, Symfony2 says that the asset is missing.
I have never worked with SCSS. How do I achieve that on file change of my scss file the css file gets created/updated and Symfony2 no longer indicates a missing asset for my css path in my HTML file?
What I have done so far in order to solve this problem:
I installed SASS and Compass via the Ruby gem manager.
In PHPStorm, I configured a file watcher "SCSS", referencing to C:\Ruby\bin\scss.bat
After installing SCSS compiler and setting the right path in File Watcher settings your css will be generated every time you change your .scss file. This .css file is located by default near original .scss and so to copy it into the web directory you need manually copy it every time or configure your file watcher to do it for you instead.
Also you can configure scssphp assetic filter to do it instead of PhpStorm. It will generate new css file every time you run php app/console assetic:dump command or every time asset is loaded via assetic controller.
To work with Assetic you need next code:
{% stylesheets filter="scssphp" output="css/myproject.css"
#MyBundle/Resources/myproject.scss
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
Related
I was given a Laravel project and I manage to download it's configuration and get it started on php artisan serve.
When I open it on localhost:8000 It opens but only the html portion of it.
It seems like css files didn't load for it.
Is there some sort of package we need to install with composer in my environment.
To get the css working.
The public folder looks like this in the laravel project.
backend favicon.ico front images index.html index.php js robots.txt sql vendor
web.config
Laravel provides a helper function, asset(), which generates a URL for your assets. You can use this in blade syntax.
Put your css, js files on public folder. for example of a css file, put it on :
../public/bootstrap/css/bootstrap.min.css
In your blade you can access this file from header like this :
<link href="{{ asset('bootstrap/css/bootstrap.min.css') }}" rel="stylesheet" type="text/css" >
hope this help
I have symfony 4.1 installed via composer and the asset component.
I have a css file at assets/css/dashboard.css and in my templates/base.html.twig I included this:
{% block stylesheets %}
<link href="{{ asset('css/dashboard.css') }}" rel="stylesheet" />
{% endblock %}
I haven't modified anything, but somehow the template is not getting called.
I tried many variations of the path, adding slashes, dots thinking maybe the path is wrong but nothing.
The css file has no issue, I dumped it's contents and pasted it inside my template's <style></style> tags and it works.
I don't know what is going on
For short:
The asset() function points on your public folder.
So in your example, your dashboard.css should be in public/css/dashboard.css than this <link href="{{ asset('css/dashboard.css') }}" rel="stylesheet" /> should work.
The longer explanation:
Usually you will structure your scripts, styles and images in your assets folder. But in production you don´t need good readable css/less/scss/js code and so you want to minify it.
And the minified (uglified) code should be copied to your public folder.
So you want to use Webpack Encore to minify your code and "deploy" it to your projects public folder.
In symfony´s documentation you can find a simple Example how to use Webpack Encore.
Why the assets are going to the public folder?
During the security concept of symfony (and the most other frameworks) the public folder is the only accessible folder. So everything the browser have to read goes there. In case of symfony your styles, scripts, images and so on.
I'm a web newbie programmer,
I'm trying to learn from home to make my own web.
I have notions of php, html, js and css.
But I've found something that is not how to solve.
I'm trying to use Composer to manage Bootstrap. I installed Composer and I have run this line of code
composer require twbs/bootstrap
that has dropped a folder with files.
I do not understand is how I make html links to find the js and css files, you should do indicating the full path?
vendor / twbs / bootstrap / dist / js / bootstrap.js
Excuse me if the question is stupid but I do not know how I should continue.
Amd excuse my English, I'm learning too but by now I use google translate
You could use a post update command in the composer.json file:
"scripts": {
"post-update-cmd": [
"rm -rf public/bootstrap",
"cp -R vendor/twbs/bootstrap/dist public/bootstrap"
]
}
And then just include the javascript- and css-files like this:
<script type="text/javascript" src="{{ ROOT_URL }}bootstrap/js/bootstrap.min.js"></script>
<link href="{{ ROOT_URL }}bootstrap/css/bootstrap.min.css" rel="stylesheet">
Yes, Composer downloads the dependencies by default into the vendor folder. So Bootstrap will also land in the vendor folder, which is not the correct place to reference it or include it.
composer require twbs/bootstrap ➔ vendor/twbs/bootstrap/dist/js/bootstrap.js
Your next step would be to write a little helper script to copy the Boostrap files you need, into your public/assets folder. You could copy the complete dist folder including sub-folders (vendor\twbs\bootstrap\dist) into public or public\assets.
Please overwrite existing files, e.g. if a file exists remove it, then copy it. This allows to easily update the files, when you need to update the Bootstrap vendor package again.
Of course, you could also just copy the files manually or create a symlink. It depends.
That gives you the following directory structure:
public
\- assets
|- css
|- js
\- fonts
\- index.html
When the Boostrap assets are copied you can start to include them, in your index.html or template (assets\js\bootstrap.min.js, etc.).
Referencing: https://stackoverflow.com/a/34423601/1163786 which shows also other solutions to this problem, e.g. fxp/composer-asset-plugin, bower, grunt.
Unless you need customization inside bootstrap (e.g. building scss), the most simple solution is relying on a CDN (as a bonus, you get super-fast caching of assets)
So, simply call your assets like so:
<link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script src="//code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
Composer is a super-excellent tool for backend dependencies, but not the best one for frontend.
If you want to have the files in your server, and you don't want to use npm, grunt or another frontend library manager, you can simply download the files listed in Massimiliano's answer and put them inside your js/css folders:
First download these files (updated to the most recent Bootstrap 3 version):
http://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css
http://code.jquery.com/jquery-2.2.4.min.js
http://netdna.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js
And put them in these folders (starting from the root of your project):
public/css/bootstrap.min.css
public/js/jquery-2.2.4.min.js
public/js/bootstrap.min.js
Now, you want to be able to call them in the blade templates for your views. It's simple: just add <link> and <script> tags as normal for any css/js file, adding the following to your blade template:
<link href="{{ url('css/bootstrap.min.css') }}" rel="stylesheet" type="text/css" />
<script src="{{ url('js/jquery-2.2.4.min.js')}}"></script>
<script src="{{ url('js/bootstrap.min.js')}}"></script>
P.s.: if you see the console, it shows something as the following error message:
Source map error: request failed with status 404
Resource URL: http://localhost:8000/css/bootstrap.min.css
Source Map URL: bootstrap.min.css.map
This will not affect the style of your page, and you can simply ignore this message, or remove a line from the bootstrap file as the answers to this question suggest. This answer explain a bit more.
My Symfony2 project has a composer.json file which references the CKEditor package:
"require": {
...
"ckeditor/ckeditor": "4.4.*"
},
When I include the main Javascript file in my Twig view:
{% javascripts
'../vendor/ckeditor/ckeditor/ckeditor.js'
%}
<script src="{{ asset(asset_url) }}"></script>
{% endjavascripts %}
it renders a script path with the following URL:
http://myproject.local/app_dev.php/js/1c365ca_ckeditor_2.js"
This would be fine if it was a standalone Javascript file, but ckeditor.js is unable to reference its many other dependencies in the vendor/ckeditor directory. Even if I explicitly included them all with assetic, I'd be unable to update their relative paths so that they could be seen by ckeditor.
I'm sure this issue applies with all large Javascript libraries. How is this normally approached?
I had exactly the same problem and I think assetic is not the best tool when dealing with third party standalone libraries, which are not integrated in bundles.
So I found a workaround by creating a symboling link to ckeditor vendor directory in the web directory of my symfony project:
cd web
ln -s ../vendor/ckeditor/ckeditor/ ./ckeditor
Then in my twig template I call the CKeditor script like this:
<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
// Change CKeditor basepath configuration
CKEDITOR.basePath = "/ckeditor/";
// Replace the <textarea id="editor1"> with a CKEditor
// instance, using default configuration.
CKEDITOR.replace( 'editor1' );
</script>
Have a look on the CKEditor bundle : Github - Packagist
This bundle is an integration of CKEditor for Symfony.
You can insert CKEditor area in your form.
I am looking at installing this theme in my SF2 project, and I note that there are 4 files at the top of the page to download, namely:
bootstrap.min.css
bootstrap.css
variables.less
bootswatch.less
I have the less.php (oyejorge/less.php) parser successfully installed along with bootstrap itself, and I have Assetic setup compressing all my CSS together and outputting this to the page:
<link href="/app_dev.php/css/425e28c.css" type="text/css" rel="stylesheet">
However, I would like to install the Superhero theme in the link above but I can't find any tutorials on how to do this? Is it just a case of overwriting variables.less and importing bootswatch.less?
Also read the Usage and Customization sections of the docs at https://github.com/thomaspark/bootswatch.
If you want to use the theme as is (without customization) you only should include the bootstrap.min.css OR bootstrap.css into your HTML:
<link href="/bootstrap.min.css" type="text/css" rel="stylesheet">
Alternatively you can compile your own version of the theme:
Download Bootstrap's source files at http://getbootstrap.com/getting-started/#download
In the source files replace the less/variables.less file with that download from bootswatch
copy the bootswatch.less into the less directory of the bootstrap sources files
Than, open the less/bootstrap.less and add the following line of code at the end of this file: #import "bootswatch"
compile the modified bootstrap as described at: Error while executing assetic dump (parse error: failed at `&:extend(.clearfix all);`)
Also notice that Bootstrap adds the autoprefixer for vendor prefixes into their build process. The vendor prefix mixins in less/mixins/vendor-prefixes.less are deprecated as of v3.2.0. So you should add https://packagist.org/packages/bit3/assetic-autoprefixer to your assetic configuration too.