Giter Site home page Giter Site logo

lua_to_html's Introduction

lua_to_html

Lua to HTML

Lua to HTML is a template engine that translates lua tables into HTML code.

For example:

{'div', class='container', {
    {'h1', 'Hello World!'},
    'Text without tags'
}}

Will be rendered to:

<div class="container">
    <h1>Hello World!</h1>
    Text without tags
</div>

Get Started

Installation

Installing lua_to_html is simple. Just type on your terminal:

luarocks install lua_to_html

Usage

To use this package just open a .lua file and import it on your project

local lua_to_html = require('lua_to_html')

Rendering

There is only one important function on this module which is "translate".

It receives as parameters: a table and a boolean It returns a string containing the html code.

The table is the code to be translated, and the boolean should be true if the result must be idented or false if not.

local html = lua_to_html:translate(table_name, true)

Templating

The table to be used will be rendered this way:

  • for each element on table
  • the first item will be the tag name
  • the last item will be its children
  • the named values will become the tag attributes
  • if the last item is not a children, the tag will self close
  • if the last item is a "false" the tag will not self close with /

This is basically the whole logic behind. Here is a demonstration that should include all possibilities:

local testpartial = require('./partials/testpartial')
local IndexPage = {
    {'!DOCTYPE html', false},
    {'html', lang='en-US', {
        {'head', {
            {'meta', charset='utf-8', false},
            {'title', 'title'}
        }},

        {'body', {
            {'h1', class='any', 
                'Sample text'..
                '<br>all what I always wanted!'
            },
            testpartial,

            {'p', {
                'Text with no tags', 
                {'span', 'text on span'}
            }},

            [[<h4> Pure html tags </h4>
            <p> Might be usefull for integrations </p>]]
        }}
    }}
}

return IndexPage

And here is how i made the partial view:

local testpartial = 
    {'div', class='test', {
        {'h1', 'Title on partial'},
        {'h2', 'Subtitle on partial'},
        {'p', {
            'Sample text on partial',
            {'span', 'span on partial'}
        }}
    }}

return testpartial

Then on a main file, I just printed the results

local lua_to_html = require('lua_to_html')
local index = require('./index')

print(lua_to_html:translate(index, true))

lua_to_html's People

Contributors

carlosmpv avatar

Watchers

James Cloos 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.