An open API service indexing awesome lists of open source software.

https://github.com/skito/atmf-js

Javascript support for ATMF
https://github.com/skito/atmf-js

atmf crossplatform format javascript js nodejs open-source react template-engine

Last synced: 8 months ago
JSON representation

Javascript support for ATMF

Awesome Lists containing this project

README

          

[Back to main document](https://github.com/skito/ATMF-JS)
# ATMF-JS / Browser enviropment

__Javascript support for ATMF (Advanced-Template-Markup-Format)__

![GitHub release (latest by date)](https://img.shields.io/badge/production-ready-green)
![GitHub release (latest by date)](https://img.shields.io/badge/coverage-100%25-green)

 

# Setup
Simply load the ATMF browser library in your project. Then set the autodiscovery.
```html

window.onload = function () {
// Configure auto discovery
ATMF.SetTemplateDiscoveryPath('../common/templates/', 'html');
ATMF.SetCultureDiscoveryPath('../common/culture');
ATMF.SetCulture('en-US');
};

```
You can now start using ATMF. You can do so by adding ``data-atmf`` attributes to your DOM elements or by using the global selector function ``__()``. Let's try something simple. Add this code.

```html


```

Then load the page, open the browser console and type:
```javascript
__('$hello', 'Hellow World!')
```

The ATMF browser library will watch all of your body DOM and will automatically evaluate ATMF markup as long as it's available in any ``data-atmf`` attribute.

 

# Advanced Usage
__Global selector ``__()``__

```javascript
// Assign variables
__('$fullname', 'Advanced-Template-Markup-Format');
__('$shortname', 'ATMF');
__('$pagetitle', '{$fullname " (" $shortname ")"}');
__('$slogan', 'Cultural made easy!');
__('$userData', __escape('{$crossScripting}'));

// Using ATMF native properties
ATMF.vars['slogan'] = 'Cultural made easy!';
ATMF.DiscoverTemplate('header').then(() => { /* Do something */ });

// State management
var setTime = function () {
__('$timeNow', __('/date "H:i:s"'));
};
setTime();
setInterval(setTime, 1000);
```

 

# Usage Front-End (data attribute)
```html


```

__Language resources__
```html



```
Translations available at ``demos/common/culture/`` folder.

__Templates and functions__
```html




{@page.title}


{#if $someVar}
Show me in some condition

{#else}
Otherwise show me

{#end}


```

__Extensions__
```html


```

 

# Usage Front-End (templates)
__Variables__
```html

{$pageTitle}


```

__Language resources__
```html

{@page.title}


{@page.theFox 10 red}


```
Translations available at ``demos/common/culture/`` folder.

__Functions - #each #if #else #end__
```html


Author Name
Books
Sold Out

{#each $authors as $author}


{$author.firstName " " $author.lastName}



    {#each $author.books as $book}
  • {$book}

  • {#end}



{#if $author.soldOut}
Sold out
{#else}
In stock
{#end}


{#end}

```

__Extensions__
```html

Today date is {/date "M d, Y"}


```

__Escaping with backslash__
```html

\{@page.title}


```

Full demo available ``demos/common/templates/`` folder.

 

# Custom Extensions
Declare your custom extensions with this interface.
```javascript
class MyCustomExtension
{
constructor() {
this.str = "My custom extension";
}

Get(args)
{
return this.str;
}

Set(args, value)
{
this.str = value;
}

static Register(ATMFEngine) {
ATMFEngine.extensions.Register('mycustom', new MyCustomExtension());
}
}
```

Then in your ATMF setup code, call the register function
```javascript
window.addEventListener('load', (e) => {
MyCustomExtension.Register(ATMFEngine);
};
```

Using the extension
```html

```

Using with JS selector
```javascript
__('/mycustom') // Returns "My custom extension"
__('/mycustom', 'Another custom value') // Change the value
__('/mycustom') // Returns "Another custom value"
```
For more advanced example with arguments check the date core extension inside ``src/atmf/engine/ext/date.js``

 

# Prototyping
To make your own custom template and culture discovery you need to prototype the ATMF engine. [Learn more about it here](https://github.com/skito/ATMF-JS#prototyping).