SlapOS Home SlapOS

    How To Add A Template

    How to Add A Template
    • Last Update:2022-09-23
    • Version:004
    • Language:en


    • Add Template
    • Update Software Profile
    • Update Instance Profile
    • Verification

    This is how to describe the process of adding a template to be used by a service. To illustrate the process this tutorial adds an index.html file for our html5as service to provide.

    Here is the commit introducing this functionality to html5as Software release: commit diff.


    Access Theia

    You can access the Theia using the url and logging in with username and password.

    Create Template

    Create in slapos/software/html5as-base/templates/ directory by right-clicking templates and selecting New File.

    Edit Template

    Open and copy the script into it:

    {% if title %}
    <h1>{{ title }}</h1>
    {% endif %}
    <p>Hello World</p>

    Add [template_index_html] section

    Add [template_index_html] section into software.cfg:

    recipe =
    url = ${:_profile_base_location_}/${:_update_hash_filename_}
    _update_hash_filename_ = templates/
    md5sum = d57cb01df5941e139b02a2f7bdabcdc8

    Make sure the md5sum is correct by running:

    $ cd ~/srv/project/slapos/software/html5as-base/
    $ md5sum templates/

    Add context key in template-cfg

    Add context key in [template-cfg]

    context =
      key template_index_html_target template_index_html:target


    link to template

    Add it in profile [profile-common] section

    template_index_html = {{ template_index_html_target }}

    This file is responsible to render the instance file. So it needs to have the path of the template.


    Render template

    Simple command to put default index.html content in the docroot

    recipe = plone.recipe.command
    command = rm -r ${html5as:docroot}/*; cp ${default_index_html:rendered} ${html5as:docroot}/
    recipe = slapos.recipe.template:jinja2
    template = {{ parameter_list['template_index_html'] }}
    rendered = ${directory:srv}/index.html
    title = {{ parameter_dict['title'] }}
    context =
      key title :title

    A new section [default_index_html] is added to render the template. It gets the path of the template by being rendered by which got its path from software.cfg.

    The template is being rendered with the context key title taken from the parameter title.

    Update md5sum

    in software.cfg

    Run the command:

    $ cd ~/srv/project/slapos/software/html5as-base/
    $ md5sum

    Copy the result in the md5sum key of [instance_html5as] section of software.cfg

    Update md5sum

    in software.cfg

    Run the command:

    $ cd ~/srv/project/slapos/software/html5as-base/
    $ md5sum

    Copy the result in the md5sum key of [template-cfg] section of software.cfg


    1. Inspect the logs
    2. Verify Index Template
    To verify if the template is added, the software is required to re-compile and the instance should be re-instantiated. Then we can access Url provided in connection parameter with curl, the content contains your title in <h1> brackets

    Re-compiling and re-instantiating

    $ slapos node software --all
    $ slapos node instance --all

    slapos node software and slapos node instance are the commands for compiling and instantiating. Before instantiating please make sure that the compilation is completed.

    Inspect the logs

    Verify the output of 

    $ slapos node software --all
    $ slapos node instance --all

    to make sure the compiling and instantiating are completed.

    Before instantiating please ensure that the compilation is completed.

    Verify Index Template

    Call the request script to capture the connection parameters:

    $ cd ~/srv/project/slapos/software/html5as-base/
    $ bash ../../../ 

    Then you can verify the index template from server_url by

    $ curl your_server_url

    As long as you see the title parameter passed from the request script on the output of curl, it means the new template is taken into account.