Portlet
Portlet table is a system table, if you want to know more about it, please check the System Tables documentation.
Overview
- Portlets allow you to create dynamic HTML content and re-use it in Pages and portlets.
- Portlets allow you to get data from the project tables using queries.
- Portlets support giving values to queries that use parameters via custom python code.
- Portlets allow you to modify the results of the queries if needed via custom python code.
Columns
name
Your portlet name, this field will be used to identify your portlet when using MakerMike renderer and Jinja2 renderer
query
In the UI this column will appear as a dropdown, it will list all your current queries from Query table with the following format <ID> <NAME>, once you pick one, it will automatically populate the drop down Insert variable available in the template column filed.
custom python pre
Mainly you can use this column to pass values to a query that uses parameters, this column has a dedicated environment see Reserved keywords in custom python columns section.
custom python post
Mainly allows you to modify query results, this column has a dedicated environment see Reserved keywords in custom python columns section.
HTML columns
The HTML columns can render HTML code, you can use a set of multiple variables to add data to your HTML code from different sources such as Image resources,Text resources, query results from Query tabled, check the sections MakerMike render and Jinja2 Render to know how to access the values mentioned depending on which render type you choose.
The HTML columns are the following:
pretemplatetweenpost
The template column will be appended in the final portlet result depending on the number of results returned by the query, the tween column will be appended after every template section but only when there is more than one template section.
It is important to know that the columns pre, template tween, and post will be joined in that order when the portlet is rendered.
Special features in UI
-
Insert Variable: this dropdown will list all columns from the query that has been picked, if you click on one option, a MakerMike variable tag will be inserted, it looks like:Example with MakerMike renderer:
<mm-column><SELECTED_QUERY_COLUMN><mm-column>Example with Jinja 2 renderer:
{{<SELECTED_QUERY_COLUMN>}}When the portlet is being rendered the tag will be displayed as the column data from its respective table.
-
Insert image resource: this dropdown will list all available image resources by name. Insert text resource: this dropdown will list all available text resources by name.
Reserved keywords in custom python columns
The custom python columns have access to the following variables and python packages:
exports
This is a dictionary that allows you to declare your own variables and use them in the HTML columns, to see examples about how to access these variables in these columns check the MakerMike Renderer and Jinja2 Renderer documentation.
Usage:
exports["<VARIABLE_NAME>"] = <VALUE>
execute_query
This is a MakerMike function that lets you write and execute queries, the syntax rules are the same as in the Query table.
Usage:
your_query_result = execute_query("<YOUR_QUERY>")
The result returned by execute_query has the following format:
your_query_result[<ROW_INDEX>][<COLUMN_NAME>]
json
Python encoder/decoder, see more about here in the official Python json documentation.
markdown
The markdown.markdown function from the Python markdown library, made available by makermike-core, can be used to convert Markdown text into HTML.
Usage:
html = markdown.markdown(<YOUR_TEXT>, extensions=['tables', 'toc', 'attr_list', 'md_in_html'])
All extensions from the library are available. For a complete list, see the official Python-Markdown Extensions documentation.
maker_mike_version
String which value is the current MakerMike version of the instance.
path_segments
Object/dictionary contains all the path segments names and values as key and value pairs
portlet_and_query_row
Object/dictionary, each key corresponds to the columns of Portlet table and Query, each value corresponds to the current values of the portlet used and its selected query.
This is a custom print function different from Python print function, you can use it for debug purposes, for example you can print variables as long as they are string type and only accepts one parameter, you can see the output when visualizing the portlet result.
Usage:
print(<STRING_VALUE>)
query_parameters
Object/dictionary, used to pass parameters to the query, the key must correspond to the selected query id, the value should be the parameters of the query inside a list.
Usage in custom python pre:
query_parameters = {
"<ID>": [<PARAMETER>, ..]
}
ID as integer
query_parameters = {
<ID>: [<PARAMETER>, ...]
}
Where <ID> corresponds to the query id in Query table, you can see it in the lists of queries in query column too, <PARAMETER> must be always a string type value, otherwise it can cause an error output when executing the portlet.
You can pass multiple parameters in the list, they have to be separated by comma. In the custom python post column the variable is available too, but only for read purposes.
response
Object that can be used to create your own responses with headers and cookies.
To create a response use the function:
response.set_response(args)
args:
response: Optional argument, it is the content that you want to return and must be a string, if no value is established the response will be the portlet content.headers: You can set multiple headers in a key/value pair where the key is the header name and the value is the header value.cookies: You can set multiple cookies in the following format:cookies=[{name: "<COOKIE_NAME>", value: "<COOKIE_VALUE>", expires="<SECONDS>", path="<COOKIE_PATH>", domain="<COOKIE_DOMAIN>"}, ...]
Other methods
get_cookies(): Returns all cookies if no cookies were set the value returned will be an empty list.get_headers(): Return all the headers set, if no headers were set the value returned will be an empty dictionaryget_response(): Returns the value established for theresponseargument.get_code(): Returns the value established for theresponseargument.-
set_cookie(args): You can set a cookie. args:- name: The name of the cookie.
- value: the value of the cookie, the default value is an empty string.
- expires: expiration time in seconds.
- path: The path of the cookie, the cookie will only work in the designed path.
- domain: The domain of the cookie, the cookie will only be available in this domain.
-
set_header(args): You can set a cookie. args:- name: The name of the header.
- value: the value of the header.
redirect
Object that can be used to make a redirection to another page.
To create the redirection use the following function:
redirect.set_response(args)
redirect has the same methods and parameters as response has, for redirect the response value must be the path to the page that you want to redirect.
request
Object/dictionary from Flask, to access incoming request data, you can check more about it at Flask request object.
user_data_in_current_project
Object/dictionary which contains all the data present in the User table of the current user, each key corresponds to a column name of the table, only for read purposes.
python builtins
Python builtins with the exception of the following functions [breakpoint, credits, compile, copyright, eval, exec, exit, help, input, memoryview, open, print, property, license, quit]
Reserved keywords only in custom python post
query_result is a list of objects/dictionaries, each object/dictionary in the list corresponds to a result row from the query selected.
Example:
query_result[<ROW_INDEX>][<COLUMN_NAME>]
Portlet accessibility
Make sure that you have set the proper roles in the read permissions of:
- The column
htmlin thePagetable. - The column image of
Image resourcetable if you use image resources. - The column text of the
Text resourcetable if you use text resources. - All columns of the
Querytable if you use a query. - For the tables used by the selected query in the
querycolumn, depending on what columns and tables the query uses, the queries executed by the functionexecute_querywill need the same permissions.