Esempio n. 1
0
def load_template(template, *args, **kwargs):
    import_asset = '% from kokoropy.asset import JQUI_BOOTSTRAP_STYLE, ' +\
    'JQUI_BOOTSTRAP_SCRIPT, KOKORO_CRUD_STYLE, KOKORO_CRUD_SCRIPT\n' +\
    '% from kokoropy import html as HTML'
    # modify kwargs
    if 'BASE_URL' in request and request.BASE_URL is not None:
        kwargs['BASE_URL']  = request.BASE_URL
    else:
        kwargs['BASE_URL']  = base_url()
    kwargs['RUNTIME_PATH']  = runtime_path()
    kwargs['APP_PATH']      = application_path()
    kwargs['REQUEST']       = request
    # modify args
    args_list = list(args)
    # add \n to prevent template rendered as path
    if not '\n' in template:
        template = template + '\n'
    template = import_asset  + '\n'+ template
    # create block pattern
    block_pattern = r'{%( *)block( *)([A-Za-z0-9_-]*)( *)%}((.|\n)*?){%( *)endblock( *)%}+?'
    # get block_chunks
    block_chunks = re.findall(block_pattern, template)
    # remove all literal block from template
    template = re.sub(block_pattern, r'', template)
    # get template by rendering content
    args_list.insert(0, template)
    args = tuple(args_list)
    template = _bottle_template(*args, **kwargs)
    for chunk in block_chunks:
        block_name = chunk[2]
        block_content = chunk[4]
        # change {% parent %} into % __base_block_BLOCKNAME()\n
        block_content = re.sub(r'{%( *)parent( *)%}+?',
                               r'\n% __base_block_'+block_name+'()\n',
                               block_content)
        template = '\n% def __block_' + block_name + '():\n' + import_asset + '\n' + block_content + '\n% end\n' + template
    # change 
    #    {% block X %}Y{% endblock %}" 
    # into 
    #     % def __base_block_X:
    #         Y
    #     % end
    #     % setdefault('__block_X', __base_block_X)
    #     % __block_X()
    template = re.sub(block_pattern, 
                     r'\n% def __base_block_\3():\n' + import_asset + r'\n\5\n% end\n% setdefault("__block_\3", __base_block_\3)\n%__block_\3()\n',
                     template)
    # render again
    args_list[0] = template
    args = tuple(args_list)
    return _bottle_template(*args, **kwargs)
Esempio n. 2
0
def template(*args, **kwargs):
    '''
    Get a rendered template as a string iterator.
    You can use a name, a filename or a template string as first parameter.
    Template rendering arguments can be passed as dictionaries
    or directly (as keyword arguments).
    '''    
    if not request.BASE_URL is None:
        kwargs['BASE_URL'] = request.BASE_URL
    else:
        kwargs['BASE_URL'] = base_url()
    kwargs['RUNTIME_PATH']      = runtime_path()
    kwargs['APPLICATION_PATH']  = application_path()
    # adjust args[0]
    path_list = list(args[0].split('/'))
    if len(path_list) >= 2 and path_list[1] != 'views':
        path_list = [path_list[0],] + ['views',] + path_list[1:]
        args_list = list(args)
        args_list[0] = '/'.join(path_list)
        args = tuple(args_list)
    return _bottle_template(*args, **kwargs)