Coder Social home page Coder Social logo

ring-tutor's Introduction

Ring-Tutor

Ring middleware to add tutorials to your ring websites.

Automatically discards completed steps and makes sure the user is at the correct URI.

Johannes Kepler

Usage

Add [org.clojars.frozenlock/ring-tutor "0.1.2"] to your project.clj dependencies.

This library also requires the ring.middleware.session middleware to be used in your ring-app.

Adding the Middleware

Make sure to add the ring-tutor middleware inside the ring-session middleware:

(def app
  (-> app-routes
      (tutor/wrap-tutor); <--- tutor middleware
      (handler/site)))  ; <--- session middleware

Making Tutorial Steps

A tutorial is nothing else than a sequence of maps:

(def demo-tutor
  [{:uri "/2"
    :step-fn #(str "<br><div id='tutor-step-2'>Tutor for " (:uri %) " !!</div><br>")}
   {:uri "/"
    :step-fn #(str "<br><div id='tutor-step-1'>Tutor for " (:uri %) " !!</div><br>")}])

(because we pop each step, the tutorial is in reverse order)

The 3 possible keywords for each step maps:

  • :uri: is the expected URI for the given step;
  • :step-fn: the function to generate the current tutorial step (with the ring request as the argument);
  • :wrong-uri-fn: the function to generate an 'error' message if the user finds himself at the wrong URI (again, with the ring request as the argument). If nothing is provided, default to the user provided error function (set with set-default-wrong-uri-fn) or to the library default.;

Adding and Removing Steps in the User Session

Any updates to the tutor state must be done with the ring response.

(POST "/tutor" []
    (-> (resp/redirect "/") ; create the ring response
        (tutor/set-tutor-sequence demo-tutor)))
(POST "/clear-tutor" []
    (-> (resp/redirect "/")
        (tutor/set-tutor-sequence nil))) ;; this clears the tutorial

To get access to the generated content in your webpages, use the function generated-tutor with the request as the argument.

(GET "/" req (str "<h1>Hello World</h1><br><a href=/1>next page</a>" (tutor/generated-tutor req)))

A nice place to use this function is in the common rendering function we usually find in the ring/compojure projects.

License

Copyright © 2014 Frozenlock

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

ring-tutor's People

Contributors

frozenlock avatar

Watchers

 avatar  avatar  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.