Skip to content

Example implementation on top of Twig template engine, for more control over our API Response.

License

Notifications You must be signed in to change notification settings

oggeh-dev/twig-extension

Repository files navigation

OGGEH Twig Extension

This is a free extension introduces our example implementation on top of Twig template engine, for more control over our API Response (conditional statements, loops, and custom functionality).

You need a Developer Account to obtain your App API Keys, if you don't have an account, request one now.

Getting Started

  1. First, you need to install Twig
composer install
  1. Next, you need to enter your App API Key in index.php
OGGEH::configure('domain', 'domain.ltd');
OGGEH::configure('api_key', '[APP_API_KEY]');
  1. For local environment, you need to enter your App Sandbox Key as well in index.php, and set sandbox setting to true
OGGEH::configure('sandbox_key', '[APP_SANDBOX_KEY]');
OGGEH::configure('sandbox', true);
  1. Optionally, you can configure your own Frontend dictionary for translating page custom model attributes as follows:
OGGEH::configure('i18n', array(
  'category'=>array(
    'en'=>'Category',
    'ar'=>'التصنيف'
  ),
  'client'=>array(
    'en'=>'Client',
    'ar'=>'العميل'
  )
));
  1. Edit your hosts file and append:
127.0.0.1 app.domain.ltd
  1. Preview example template in browser at http://app.domain.ltd

IMPORTANT

You will not be charged for your apps in development mode. Please do not use Sandbox headers in production mode to avoid blocking your App along with your Developer Account for violating our terms and conditions! If you're planning to use this example, remove the SandBox header from JavaScript (assets/js/main.js @line 109)

How it Works

The library accepts the following URL Segments:

http://domain.ltd/?lang=&module=&param1=&param2=

If you're familiar with apache rewrite rules, you can rename htaccess.txt to .htaccess which redirects all requests at your Frontend Template to the above index file as follows:

http://domain.ltd/:lang/:module/:param1/:param2

Remember to uncomment rewrite settings at index.php before activating this file, in addition to all URLs in your template files (including javascript if necessary).

URL Segment Description
domain.ltd Your App domain as entered during creation.
:lang URL language code (for example: en), this is how you pass target language to our API Requests.
:module Represents which content model you want to retreive from your customer's content (page, album, .. etc).
:child_id Represents additional filtering parameter to the selected model (for example: page-unique-identifier).
:extra_id Represents an extra parameter to the selected model.

The extension maps each model from your Frontend Request URL above to an HTML template file inside the tpl directory.

As of the home page, you need to keep a default HTML file tpl/home.html.

  • You can use tpl/404.html for invalid requests.
  • You can use tpl/inactive.html to be displayed when your App is not in production mode.

The extension supports the following functions:

Function Description
call('json') Makes stacked API request using php curl.
get('alias') Retrives individual API response by method alias defined for each request method.
trans('phrase') Translates custom phrases defined at index.php.
flag('lang') Maps the language code to a country code (defined at locale.json).

The extension supports the following filters:

Filter Description
urldecode Decodes URL-encoded string.

Usage

There're 2 more files you need to keep for proper usage:

  1. tpl/api.twig: defines global API methods, mostly reused in header and footer blocks, and extends those based on current module from URL.
  2. tpl/blocks.twig: define both header and booter blocks to be used later on each module template.

At any given module template (home, page, news, album, contact, or search), you should keep the following structure:

{# Import global wrapper #}
{% extends 'api.twig' %}

{% block api %}

    {# Fetch response from method alias `app` to be used accross all blocks #}
    {% set app = get('app') %}

    {# Reuse common blocks (i.e. header and footer) #}
    {% use 'blocks.twig' %}

    {# Print header block #}
    {{ block('header') }}

    {# Print content block #}
    {% block content  %}

    	{# Your module template goes here  #}

    {% endblock %}

    {# Print footer block #}
    {{ block('footer') }}

{% endblock %}

The above sequence enables you to make only one stacked API request and retrieve individual responses, for example:

{% set contact = get('contacts') %}
<ul>
{% for contact in contacts %}
    <li>
        {{ contact.name }}: {{ contact.email }}
    </li>
{% endfor %}
</ul>

Where contacts is the alias for the following API request:

curl -H "Content-Type: application/json" -X POST -d '[{"alias":"contacts","method":"get.contacts","select":"name,email"}]' https://api.oggeh.com/?api_key=[APP_API_KEY]&lang=en

API Documentation

See API Reference for additional details on available values for select attribute on each API Method.

Template in Use

Template in Use Editorial by HTML5 UP

Template License Creative Commons Attribution 3.0

Template Credits Built by AJ - Modified by OGGEH Cloud Computing

Photos used

unsplush.com

About

Example implementation on top of Twig template engine, for more control over our API Response.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages