How can I include css files based on frontmatter in blogposts? - html

I have the following code in my skeleton layout.
{% if page.customcss%}
<link rel="stylesheet" href="{{page.customcss}}">
{% endif %}
And in pages I mention the customcss in the frontmatter like this:
---
layout: skeleton
customcss:
- 'assets/css/something.css'
---
This works for normal pages but for blog posts, jekyll generates then in a different directory and can't access the css and js files:
_site
-2021/1/1
-blog.html
-assets
-css
-something.css
-js
-x.js
for js files I've solved it with %link in the skeleton.html layout file:
<script src="{% link /assets/js/mirai.js %}"></script>
But it doesn't work for css files:
<link rel="stylesheet" href="{% link {{page.customcss}} %}">
`render': Could not find document 'assets/css/something.css' in tag
'link'. (ArgumentError) Make sure the document exists and the path is
correct.
I assume jekyll doesn't like the nested {{}}.
Is there a way to keep page.customcss while allowing usage in blogposts? I do not want to make a separate skeleton.html for blog posts.
Or am I doing something wrong here?

I found this within literal minutes. Keeping the ques up if it helps somebody:
Jekyll doesn't apply CSS to posts on GitHub Pages
basically, paths are not relative if is starts with /.
Ex:/assets/css/something.css
No need for %url.

Related

My CSS File is not applying when I link it into the HTML

I have a CSS file style.css stored in a CSS folder and tried using <link rel="stylesheet" type="text/css" href="style.css"> to link it into the HTML file. I got a 404 not found error from this and am not sure what I am doing wrong.
Below is the code (there is not much at the moment):
{% extends "base.html" %}
{% block body %}
<div class="MainBody">
<link rel="stylesheet" type="text/css" href="style.css">
<div class="w3-container w3-padding-32 w3-hide-small center-align">
<h1>About Us</h1>
</div>
</div>
{% endblock %}
This shows the full path of style.css within the project
Please let me know what is being done incorrectly.
Actually, you answered your own question:
I have a CSS file style.css stored in a CSS folder
Your href attribute should look something like: href="/css/style.css"
The href should should have the path where the file is located along the file name not only the file name.
Example :
<link rel="stylesheet" type="text/css" href="./css/style.css">
we use "./" to search in the current folder.
we use "../" If you want to come back from the current folder.
Try this:
In the href, would you not leave the first / off? I thought that would go to a root folder. Try and eliminate that first slash and see what happens. As was said, the 404 is because it's looking in the wrong place.
I see this is a structure of a Flask app. In Flask, the CSS files and other assets should by default be stored in the static directory which should be directly inside your home directory.
Your file structure should look like this:
TSAWeb_App
|__ templates
|__ index.html
|__ static
|__ CSS
|__ style.css
Then you to access it inside the web app, first import url_for method
from flask import Flask, url_for
Then you can refer to CSS file as:
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='CSS/style.css') }}">
Then when the app is run, the href tag will have the correct value set
UPDATE: If you're using Django, see this doc
Configuring static files
Make sure that django.contrib.staticfiles is included in your
INSTALLED_APPS.
In your settings file, define STATIC_URL, for example:
STATIC_URL = '/static/'
In your templates, use the static template tag
to build the URL for the given relative path using the configured
STATICFILES_STORAGE.
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image">
Store your static files in a folder called static in your app.
For example my_app/static/my_app/example.jpg.

GitHub pages Jekyll - issues with CSS name

I'm a little bit new, but I was curious about changing the name of my main file in my css folder. I have the css folder, where I am importing my partials.
I initially tried to call this base.css and add the following line in my head.html (in my includes directory).
<link rel="stylesheet" href="{{ site.baseurl }}css/base.css">
However, it still would not load the appropriate main css file. Instead, I had to rename my file to main.css. there has to be a way around this but I wasn't sure and the literature wasn't that helpful.
Thanks so much! I would really appreciate any help.
Starting from Jekyll 3.3, you can use the relative_url filter to avoid adding baseurl, it should be available for GitHub Pages very soon.
<link rel="stylesheet" href={{ "/css/base.css" | relative_url }}>
You can use the prepend option instead:
<link rel="stylesheet" href="{{ "/css/base.css" | prepend: site.baseurl }}">
If you are using Github Pages then ensure that config.yaml includes the base.url variable and it points to your repository name, e.g:
baseurl : '/my-awesome-jekyll-site'

assetic symfony2: html content inside css files

I'm trying to import css file inside twig file using assetic.
{% stylesheets 'bundles/sfeuser/css/bootstrap.css' filter='cssrewrite' %}
<link rel="stylesheet" href="{{ asset_url }}" type="text/css" />
{% endstylesheets %}
when I load the page. it was only HTML without style
and when I checked the css file. I found out that it contains HTML content of the page loaded.
In the security.yml file I forgot to remove a firewall with authentification that was applied on the whole application.
That's why when the browser tries to access the CSS file it gets redirected to the login page. Therefore the HTML content of the login page was found in the CSS file.

404 error CSS file not found in chrome dev tools

I've spent an hour and a half reading other posts about this issue and still haven't solved it. I believe the file is in the right place and the file path reference is the appropriate one. I'm testing my web application locally trying to style it up a bit and chrome dev tools is saying that the CSS file cannot be found. I'm using Python, Google App Engine and Jinja2. Check out the error for yourself if you'd like. Thanks in advance for your time and expertise!
Here are my HTML files. The second one inherits from the first one.
base.html
<!DOCTYPE html>
<html>
<head>
<title>Chores</title>
<link rel="stylesheet" href="static/style.css" type="text/css" />
</head>
<body>
<a href="/" class="main-title">
Chores
</a>
<div id="content">
{% block content %}
{% endblock %}
</div>
</body>
</html>
front.html
{% extends "base.html" %}
{% block content %}
<br>
<br>
Do Chore
+ Add Chore
- Remove Chore
Chore Details
<br>
<br>
<br>
{% for chore in chores %}
{{ chore.render() | safe }}
<br><br>
{% endfor %}
{% endblock %}
My project directory is structured like...
project-name/
main.py
templates/
HTML files
static/
style.css
Here's my app.yaml...
application: chorestimemadison
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
- name: jinja2
version: latest
You're missing a handler for the static directory:
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /static
static_dir: static
- url: .*
script: main.app
Learn more about why this is necessary.
I'm betting you're having an issue with directory access. If your HTML files are under the templates folder and your templates folder is the location that's being served as http://chorestimemadison.appspot.com/, then the HTML files cannot access the static folder (or the CSS files therein) no matter what URL you use because they're not part of the web server's directory structure.
You'll have to either:
Move the static folder into the templates folder.
Change the root directory of your web server to be project-name instead of templates and change the reference to style.css to ../static/style.css.
As your css and html files are in paralel folders you first need to back off 1 directory with "../" and then type your css path like so
<link rel="stylesheet" href="../static/style.css" type="text/css" />
The way you typed, it's looking for your css in templates/static, because tempalates is your current folder (where the html executes)
Use "./" instead of "/". It helps to get the current directory.
In your case, the code should be:
<link rel="stylesheet" href="./static/style.css" type="text/css" />

How to make automatic prefixes for relative urls calling css assets from any (sub)folder?

I would like to be able to define the links of my website as relative paths. I think it is more reliable in case the site is moved later for instance.
For the moment, my links are absolute, so I have:
<link rel="stylesheet" href="{{ site.url }}/assets/css/app.css" />
in _includes/header.html called by _layouts/page.html to generate the pages of my website.
A first approach would be to put
<link rel="stylesheet" href="assets/css/app.css" />
but it works only for pages in the root.
Another attempt would be to put
<link rel="stylesheet" href="../assets/css/app.css" />
but it works only for pages in a subfolder the root.
In fact we need to use as many ../ as needed to go back to the root. Would it be possible to automatically count it (maybe based on the page.url variable? I am not sure how to do it nor where to put such a script.
I am aware of the answer of kikito here but it is needed to manually declare the correct path to the root variable of the page in the metadata when creating it. I would like to automate this process.
Did you have issues with the accepted answer to the question you linked to? That solution works for me.
In the head of my layout file I include the code:
{% capture lvl %}{{ page.url | append:'index.html' | split:'/' | size }}{% endcapture %}
{% capture relative %}{% for i in (3..lvl) %}../{% endfor %}{% endcapture %}
Then my resources can be set up as:
<link rel="stylesheet" href="{{ relative }}assets/css/combined.min.css">
This results in the correct number of ../ parts being added. The "assets" directory in this example is in the root of my site.