Skip to main content

A python script for css class names conditional generation

Project description

css-class-names Build Status

A python script for css class names conditional generation. Inspered by the node package classnames.

Why?

The node classnames package has been helping me a lot with React/JSX and general javascript front-end development. I really missed something similar to use when working with django and jinja2 templates, so, I decided to create this package.

Usage

You can add css class names by calling the function with arguments as:

  • Strings
  • Numbers: Floats or Integers (0 is allowed)
  • Sequences: Lists or Tuples
  • Dictionaries
from css_class_names import class_names

class_names('header', 'is-visible') # 'header is-visible'
class_names(['is-visible', 'text-uppercase']) # 'is-visible text-uppercase'
class_names((100001000, 'clear')) # '100001000 clear'

class_names('header', { 
    'header--is-fixed': True, 
    'header--is-blue': False 
}) 
# 'header header--is-fixed'

Conditional dicts

Dictionaries can be used to concat class names conditionally using expressions:

from css_class_names import class_names

errors = ['Some error']
class_names('alert', { 
    'alert-danger': errors, 
    'alert-success': not errors,
    'small': True
}) 
# 'alert alert-danger'

class_names('client', { 
    'client-{}'.format(client.id): client, 
    'disable': not client.active()
}) 
# 'client client-989'

Excludes falsy values

Falsy values (except 0) and not supported types will be ignored:

from css_class_names import class_names

class_names([], '', False, {}, 0, object(), lambda x: x, None)
# '0'

Strip and flatten

Strings are striped and sequences are recursively flattened:

from css_class_names import class_names

class_names([' header   ', ['  green ', ' small ']])
# 'header green small'

Dedupe

You can dedupe names with the dedupe argument:

from css_class_names import class_names

class_names('cart', 'is-open', 'is-logged', {'is-logged': True}, dedupe=True)
# 'cart is-open is-logged'

Prefix

You can add class name prefixes with the prefix argument. This is really usefull when you need a class namespace or using the BEM methodology:

from css_class_names import class_names

class_names('head', 'head--is-empty', {'head--is-large': True}, prefix='mysite-')
# 'mysite-head mysite-head--is-empty mysite-head--is-large'

Development

After cloning the repository, create a virtualenv and use the Makefile commands to setup development requirements and run tests:

make setup
make lint 
make test 
make watch # run tests when code changes
make coverage

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

css-class-names-0.0.1.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

css_class_names-0.0.1-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

Details for the file css-class-names-0.0.1.tar.gz.

File metadata

File hashes

Hashes for css-class-names-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4f2d9613c51f0cf5eb63e7aa962dfb3e084ddab91f2ac0b8163272976358caf8
MD5 05294b5ce116afaaa7228c2ec5c2902b
BLAKE2b-256 301a61a2468df5d791797dc327b75022d9c028e2b16fa211d6cb65b950fe1ce2

See more details on using hashes here.

File details

Details for the file css_class_names-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for css_class_names-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e14fdff5908b23509513e8a48d180954c42ea5a98238c0dfd8e68118e4564c90
MD5 c55f95ab4f2ca44df9f4393e2367a641
BLAKE2b-256 2bd01d15a71562459ec9ce5ff1eb1b7034872fc0e93df35b26a3f127d4ddba94

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page