Re: Euphoria MVC framework - template parser online
- Posted by ghaberek (admin) Sep 24, 2018
- 1753 views
I added another example for the template engine.
The use of functions makes the code look a little busy right now, but I'll be adding "map properties" to the parser, where you retrieve nested values, e.g. item.propery_name, etc.
Heck, as it is right now, you could just use this template parser right now to separate your logic and presentation layers for a basic website.
https://github.com/OpenEuphoria/euphoria-mvc/blob/master/examples/example2.ex
-- -- This is a more advanced example showing how to use functions from within templates. -- include std/map.e include mvc/template.e sequence data = { { {"id", 1}, {"first_name", "Mommy"}, {"last_name", "Mewburn"}, {"email_address", "mmewburn0@oakley.com"}, {"job_title", "Business Systems Development Analyst"}, {"ip_address", "245.131.117.235"} }, { {"id", 2}, {"first_name", "Malinda"}, {"last_name", "Yemm"}, {"email_address", "myemm1@goodreads.com"}, {"job_title", "Social Worker"}, {"ip_address", "110.26.43.251"} }, -- SNIP -- } function get_id( object m ) return map:get( m, "id", 0 ) end function add_function( "id", {"m"}, routine_id("get_id") ) function first_name( object m ) return map:get( m, "first_name", "" ) end function add_function( "first_name", {"m"} ) function last_name( object m ) return map:get( m, "last_name", "" ) end function add_function( "last_name", {"m"} ) function email_address( object m ) return map:get( m, "email_address", "" ) end function add_function( "email_address", {"m"} ) function job_title( object m ) return map:get( m, "job_title", "" ) end function add_function( "job_title", {"m"} ) function ip_address( object m ) return map:get( m, "ip_address", "" ) end function add_function( "ip_address", {"m"} ) procedure main() sequence user_list = {} for i = 1 to length( data ) do user_list &= { map:new_from_kvpairs(data[i]) } end for object response = map:new() map:put( response, "title", "Example 2" ) map:put( response, "user_list", user_list ) sequence content = render_template( "example2.html", response ) puts( 1, content ) end procedure main()
{% extends layout.html %} {% block content %} <table> <tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Email Address</th> <th>Job Title</th> <th>IP Address (v4)</th> </tr> {% for user in user_list %} <tr> <td>{{ id(user) }}</td> <td>{{ first_name(user) }}</td> <td>{{ last_name(user) }}</td> <td>{{ email_address(user) }}</td> <td>{{ job_title(user) }}</td> <td>{{ ip_address(user) }}</td> </tr> {% end for %} </table> {% end block %}
-Greg