Skip to main content
Author:

Keryn Knight

Version:
0.2.3

Release

Status

stable (0.2.3)

travis_stable

master

travis_master

What it does

A small Django package for rendering a request’s URL as a series of CSS class names, such that one can target sections or individual parts of a site using CSS only.

It can also look for path specific CSS files, and output those for you.

Generating class names

For example, say all your blog posts are under /blog/, then you might do:

{% load path2css %}
<body class="{% path2css request.path %}">...</body>

Subsequently, going to /blog/, /blog/post/, /blog/post/comments/ etc will all have the blog CSS class added to the body, which you could then use:

body.blog {...}
body.blog-post {...}
body.blog-post-comments {...}

Note that blog-post-comments, being the deepest namespace reached, would also have the blog and blog-post classes added.

Request specific CSS files

If you have a URL like /blog/post/4/, using the {% css4path %} tag allows you to load hierarchy specific CSS files:

{% load path2css %}
<head>
{% css4path request.path %}

might output the following:

<link href="/static/css/blog.css" rel="stylesheet" type="text/css" />
<link href="/static/css/blog-post-4.css" rel="stylesheet" type="text/css" />

…if those files exist in one of your static directories. In the above example, I’ve not created a blog-post.css file, so the template tag knows not to load it for output, because it would be a 404. It should only ever include files that do exist.

This allows you to compartmentalise your CSS files into separate parts, like we used to do in the old days when websites were often hand-crafted and different sections had a different look and feel, or whatever, before the inevitable rise of CMS made things a little more homogenized.

Installation

It the stable release is on PyPI:

pip install django-path2css==0.2.3

The templatetags

In case you’re already using the class names that would be generated, {% path2css %} takes a prefix=x and/or suffix=y parameter, so that you can re-namespace things without clobbering your existing styles:

{% path2css '/blog/post/' prefix='path-' %}
{% path2css '/blog/post/' suffix='-area' %}
{% path2css '/blog/post/' prefix='pre_' suffix='_post' %}

The {% css4path %} takes the same prefix/suffix parameters, and also takes an optional directory, whose default value is css

The context processor

There’s also a context processor which may be used by adding path2css.context_processor to your existing list. It does the same thing as {% path2css %} with no prefix/suffix arguents.

Supported Django versions

The tests are run against Django 1.8 through 1.10, and Python 2.7, 3.3, 3.4 and 3.5.

Roadmap

If I ever have a need for it, a JavaScript equivalent that can consume window.location.path or whatever … and potentially bridges to things like angular, I guess?

The license

It’s the FreeBSD. There’s should be a LICENSE file in the root of the repository, and in any archives.


Copyright (c) 2016, Keryn Knight All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Change history for django-path2css

0.2.3

  • Ensured non-ascii, URLEncoded values and those which would need escaping, do not get consumed for consideration by either the {% path2css %} or {% css4path %} template tags.

0.2.2

  • Fix long description.

0.2.1

  • Exposed the split_on kwarg to the {% path2css %} and {% css4path %} template tags, allowing for splitting based on patterns other than /.

0.2.0

  • Added support for searching staticfiles directories for CSS files matching path components, see {% css4path %} for usage.

0.1.1

  • First release - tests for the template tag & context processor passing on all of the tox matrix.

Release files for django-path2css 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-path2css 0.2.3
File Size Uploaded
django-path2css-0.2.3.tar.gz 14.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-path2css 0.2.3
File Interpreter ABI Platform
django_path2css-0.2.3-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 29.4 kB

Release files / django-path2css-0.2.3.tar.gz

Download URL django-path2css-0.2.3.tar.gz
Size 14.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a8e0429afa6861528062b0e32ee0e03c8ef251fd48edb431bece63ab2cd4f2e1
BLAKE2b-256 checksum
How to use checksums
1cae4b16af23bec8b158d4262f151e59bfa21f81e60693bb405c8f09ee0e34a2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/36.2.5 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/2.7.11

Release files / django_path2css-0.2.3-py2.py3-none-any.whl

Download URL django_path2css-0.2.3-py2.py3-none-any.whl
Size 15.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
1060bfd5445e346192a8a2957e13ec72250fb362336f2973a0f9c8c0f4742916
BLAKE2b-256 checksum
How to use checksums
e3224d9834df1c80ed2cb65e2978189fc0a238993583e952aa772e4f474ba5c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/36.2.5 requests-toolbelt/0.9.1 tqdm/4.44.1 CPython/2.7.11

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page