Coder Social home page Coder Social logo

alkaline's Introduction

Alkaline short guide

Brief introduction

Alkaline is a lightweight module made to render HTML server-side using the power and the simplicity of Python.

Installation

To install Alkaline simply type:

pip3 install alkaline

It doesn't require any dependencies

Basics

So, I will start with an example: I want to display the string “Hello World” in my HTML document. There are two methods: command-line and python. In the first example we will use the command-line option, made for debugging. In the root folder, you have to create the file template.html (assuming that you already know HTML). In template.html you can write all you want, remembering to close all the tags due to XML package problems, but in this case write in the file:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body>
    <p>$hello$</p>
</body>
</html>

This is all HTML markup code, except for $hello$, in fact that is a variable. Variables in Alkaline is referenced in HTML with at the beginning and end with ‘$’ and between the variable name. But if we compile this, it will prompt an error, so what do we have to do to display the string “Hello World”? It’s very simple. We’re gonna add inside <head>, <body> or <html> a tag called <python> like so:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body>
    <p>$hello$</p>
</body>
<python>
  self.vars[“hello”] = “Hello World”
</python>
</html>

Inside <python> you can type any sort of Python code.

I told you that it was simple! But inside <python> it assign to dict key “hello” the value “Hello World”, but what does it mean? The Python code inside is executed before the effective compiling, so tells what to do before the variables been replaced. In this case tells that the variable “hello” contains the value “Hello World” and, consecutively, the compiler will replace the keyword $hello$ with Hello World. I hope I was clear.

To assign to a variable an HTML tag, you can type:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body>
    $hello$
</body>
<python>
self.vars[“hello”] = tag(“p”, “Hello World”)
</python>
</html>

The tag function is mandatory only in templates, to avoid read issues. The working scheme of tag function is this:

tag([html tag], [inner text], [attributes in dict])

Tag function returns a string, for example in the Hello World example above the result will be: “<p>Hello World</p>”. The Hello World example without and with tag function will produce the same result. To compile this template, open a terminal inside root folder and type:

python3 -m alkaline template.html

Basically we called the Alkaline module and passed the file to compile as argument. After we pressed enter, will appear the Alkaline logo and two self- explanatory sentences: rendering and done. This script will automatically detect changes and compile the file index.html. If we open index.html we’ll notice that, as expected, the segment $hello$ changed to his value, “Hello World”. Furthermore python tag has been eliminated, guaranteeing more security in other applications.

Import and use Alkaline inside Python code

First of all, create a new Python file, the name is not important. In this file write down:

from alkaline import Engine
eng = Engine(“template.html”)
print(eng.compile({“hello”: “Hello World!”}))

This piece of code will import Engine from alkaline and initialize it for the template specified inside brackets. The successive line a print function prints out the result of compile, that is in string format. Inside brackets we optionally can specify manually the value of vars in dict type. If you want to write the results in a file you can type:

open(‘index.html’, ‘w’).write(eng.compile())

It’ll produce the same result of the first chapter example.

Congratulations! Now you are good to go, I have transmitted you all the fundamental instructions to work with Alkaline. Now it’s your responsibility to use it with your creativity! Good coding!

alkaline's People

Contributors

frankigeno avatar

Watchers

 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.