jczic / MicroWebSrv

A micro HTTP Web server that supports WebSockets, html/python language templating and routing handlers, for MicroPython (used on Pycom modules & ESP32)
https://github.com/jczic/MicroWebSrv
MIT License
641 stars 116 forks source link

PyHTML with vars #24

Closed JK-de closed 6 years ago

JK-de commented 6 years ago

Added vars to WriteResponsePyHTMLFile() Now you can add variables to pyHTML-rendering like:

    vars = {}
    vars['menu'] = 'devices'
    vars['device_list'] = GetList(...)

    return httpResponse.WriteResponsePyHTMLFile('www/devices_list.html', headers=None, vars=vars)

Added FlashMessage() Add a message or error display on NEXT pyHTML-rendering. It's important for redirected URLs like:

    err, idx = Cmd.excecute(cmd)
    if err:
        msg = 'Error "{0}" - Can not add device "{1}"'.format(err, duid)
        httpResponse.FlashMessage(msg, 'danger')
    else:
        Cmd.excecute('save')
        msg = 'Device "{}" added'.format(duid)
        httpResponse.FlashMessage(msg, 'info')

    return httpResponse.WriteResponseRedirect('/devices')

with pyHTML;

  {{ if MESSAGE_TEXT }}
    <div class="alert alert-{{ MESSAGE_STYLE }} hide" role="alert">
      <strong>{{ MESSAGE_TEXT }}</strong>
    </div>
  {{ end }}

Added more flexibility to {{ if xxx }} The 'xxx' after the 'if' in pyHTML must be a bool - otherwise exception! Now:

You can check a variable before use it:

  {{ if message }}
      <strong>{{ message }}</strong>
  {{ end }}
jczic commented 6 years ago

Yes :) It's cool ! I've merged that ! 👍