Giter VIP home page Giter VIP logo

django-widgeter's Introduction

Build Status

Widgeter

A simple engine for django backend widgets.

Widgets are snippets of code that render into a template. They can be used when you need to include a template with a context that is not available in the front end.

Moreover, widgets can be added by reusable apps without the need for the main project to know what their requirements are.

Features

  • Creation of custom widgets in reusable apps
  • Rendering of widgets by name
  • Rendering of widget blocks without previous knowledge of widget names
  • Autodiscovery of widgets from all installed apps

Installation

  1. Install the package using pip install django-widgeter
  2. Add widgeter to your INSTALLED_APPS in settings.py

Defining a Widget

Add a widgets.py file on your application with the following statements.

  • Widgets can be defined with a template (and optionally a context):

      from widgeter.core import Widget
      
      class HelloWorld(Widget):
          template = 'hello_world/widget.html'
    
          def get_context(self, context, options=None):
              return { 'message': u'Hello World!' }
    

    or with a render function:

      from widgeter.core import Widget
      
      class HelloWorld(Widget):
          def render(self, context, options=None):
              return u'Hello world!'
    
    
  • Register your widget:

      from widgeter.registry import registry
    
      registry.register('hello_world', HelloWorld())
    

Rendering a Widget

On a template you first have to load the template tags and then render the particular widget:

  {% load widgeter %}
  {% widget "hello_world" %}

You can also pass variables to your widget:

  {% load widgeter %}
  {% widget "hello_world" current_user %}

Using Blocks

Widget blocks allow you to render widgets without previously knowing the names for them. The autodiscovery process will add them to the registry and you can the look them up by block name. Priorities can also be assigned to render them in order.

  • First define a block on the widget:

      from widgeter.core import Widget
      
      class HelloWorld(Widget):
          block = 'home'
          priority = '1'
          template = 'hello_world/widget.html'
    
          def get_context(self, context, options=None):
              return { 'message': u'Hello World!' }
    
  • Then, on the template, use the widget_block tag:

      {% load widgeter %}
      {% widget_block "home" current_user %}
    

Running the Tests

You can run the tests with via::

python setup.py test

or::

python runtests.py

Inspiration

This project is based on https://github.com/marcinn/django-widgets but updated for new versions of Django.

django-widgeter's People

Contributors

dlapiduz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.