PHP code example of rodnaph / morgan

1. Go to this page and download the library: Download rodnaph/morgan library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rodnaph / morgan example snippets


use Morgan\Template as T;

T::render(
    'page.html',
    array(
        '.header h1' => T::content('Some Text')
    )
);

$homePage = T::template(
    'index.html',
    function($title, $content) {
        return array(
            'h1' => T::content($title),
            '.container' => T::htmlContent($content)
        );
    }
);

$html = $homePage('The Title', '<h2>Main content...</h2>');

# set the content of an element
T::content('Some new content')

# append content to an element
T::append('This please')

# prepend content to an element
T::prepend('Some string')

# use HTML as content for an element
T::htmlContent('<b>bold text</b>')

# set attributes of elements
T::setAttr('href', '/blog/post.html')

# remove attributes of elements
T::removeAttr('class')

# add classes to element
T::addClass('foo', 'bar', 'baz')

# remove classes from element
T::removeClass('foo', 'bar', 'baz')

# replace with some HTML
T::replaceWith('<b>Some HTML</b>')

T::render(
    'file.html',
    array(
        'a' => function(DOMElement $element) { … }
    )
);

array(
    '.foo' => T::all(
        T::content('New content'),
        T::setAttr('href', '/some/page.html')
    )
)

$snippet = T::snippet(
    'blog-post-list.html',
    '.post',
    function($data) {
        return array(
            '.subject' => T::content($data['title'])
        );
    }
);

# array of blog posts to show

$posts = array(
    array(
        'title' => 'First Post',
        'summary' => 'Some short snippet',
        'href' => '/blog/post-one.html'
    ),
    array(
        'title' => 'Another Post',
        'summary' => 'And another short snippet',
        'href' => '/blog/another-post.html'
    )
);

# re-usable blog post summary snippet

$postSnippet = T::snippet(
    'blog-posts.html',
    '.post',
    function($item) {
        return array(
            'h3 a' => T::all(
                T::content($item['title']),
                T::setAttr('href', $item['href'])
            ),
            'p' => T::content($item['summary'])
        );
    }
);

# render the main template

T::render(
    'blog-posts.html',
    array(
        'h1' => 'The Blog Posts Page',
        '.posts' => T::map($postSnippet, $posts)
    )
);

use Morgan\Template as T;

# create and echo the template

$t = new T('path/to/file.html');

echo $t->html(array(
    'h1' => T::contect('Some title')
));

# snippet is exactly the same, but with a selector

$s = new T('path/to/file.html', '.some-selector');

echo $s->html(array(
    '.description' => T::content('A Description')
));