PHP Classes

MH_Widget: Generate HTML for embedded widgets from parameters

Recommend this page to a friend!
  Info   Example   Demos   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 164 All time: 8,925 This week: 49Up
Version License PHP version Categories
mh_widget 0.2MIT/X Consortium ...5.3HTML, PHP 5, Content management, Temp...
Description 

Author

This package can Generate HTML for embedded widgets from parameters.

The main class can be called directly passing the name of the widget. The class will load the respective widget class from the configured widgets directory and passes it any parameters that determine how it will render the widget HTML.

HTML pages may may contain custom tags that are parsed by a separate class that identifies the widget tags, extracts the parameters from the tag attributes and replace them by the HTML generated for the respective widget.

Example widget classes are provided for simple hello world widget and a widget to embed gravatar images.

Picture of Matteo Hertel
Name: Matteo Hertel <contact>
Classes: 2 packages by
Country: United Kingdom

 

Example

<?php

$start
= microtime(true);
require_once
"../vendor/autoload.php";

$widget = new \MHDev\WidgetCore\MH_Widget("\MHDev\Widgets\\");

/*
 * Example one
 *
 * Simple Hello World
 */

echo "<h3>Simple Hello World</h3>";
echo
$widget("Example\HelloWorld");
echo
"<hr />";

/*
 * Example two
 *
 * Gravatar
 */

echo "<h3>Gravatar</h3>";
echo
$widget("Gravatar", ["email" => "info@matteohertel.com", "size" => 80]);
echo
"<hr />";

/*
 * Example three
 *
 * test from different namespace
 */

echo "<h3>test from different namespace</h3>";
$widget->prefix = "\Test\\";
echo
$widget("TestWidget");
echo
"<hr />";

/*
 * Example four
 *
 * Static access
 */

echo "<h3>Static access </h3>";
echo \
MHDev\WidgetCore\MH_Widget::create("Gravatar", ["email" => "test@test.com", "size" => 128, "default" => "identicon"]);
echo
"<hr />";

/*
 * Example five
 *
 * Parser
 */
echo "<h3>Parser</h3>";
$widget->prefix = "\MHDev\Widgets\\";
$html = <<< EOT
This is a simple example in wich I can show you that I can get my profile pic from my gravatr widget
        <br />
    <mhwidget size="128" email="info@matteohertel.com">Gravatar</mhwidget>
        <mhwidget size="128" default="mm">Gravatar</mhwidget>
<p>&nbsp;</p>
    and I want another image but 80x80 now!
        <mhwidget config='a:2:{s:4:"size";s:3:"128";s:5:"email";s:21:"info@matteohertel.com";}'>Gravatar</mhwidget>
    and last but not least, <mhwidget>Example\HelloWorld</mhwidget>
  
EOT;

echo \
MHDev\WidgetCore\WidgetParser::parse($html);


echo
sprintf("<p>Memory allocated: %skb</p>", memory_get_usage(true) / 1024);
echo
sprintf("<p>Memory spike: %skb</p>", memory_get_peak_usage(true) / 1024);
echo
sprintf("<p>Execution time: %s</p>", microtime(true) - $start);



Details

MH_Widget

Scrutinizer Code Quality Build Status

Demo: http://test.matteohertel.uk/mh_widget/

Flexible, highly configurable widget system replace an HTML place holder with any content.

The main purpose for this package was to be used alongside a text editor like CKEditor to implement a flexible widget system for the end user(http://docs.ckeditor.com/#!/guide/widget_sdk_tutorial_1)

This package will provide a nice and neat interface to create widgets ready to use in any project, there are a lots of feature straight out of the box, but with its flexible nature can fit any need.

There are two main ways to use this package: - Use the widget system standalone as easy way to render HTML - Use the widget system with the integrated parser

To use the widget standalone:

  1. Create an instance of \MHDev\WidgetCore\MH_Widget
  2. Call the instance as a function (to trigger the magic `__invkoke`) passing two arguments (one optional): namespace, config array
  3. under the hood the class will: - create a new instance of the requested widget in the given namespace(or use the default one \MHDev\Widgets\ and pass the config object - call the instance as function (to trigger the magic `__invkoke`) and return the result

The single widget workflow is:

  1. from the magic invoke the the `controller` method is called
  2. the `controller` method call the `model` method to get data
  3. the `controlle`r will call and return the view method passing the data from the model - if the `prevent_view` kay is found in the config array the controller will return the data from the model without call the view

Using the parser

With the integrated parser you can use a custom non-standard html tag mhwidget with attributes to render content from the widget, and example of a working tag:

<mhwidget size="128" email="youremail@gravatr.com">Gravatar</mhwidget>

Under the hood

To be a proper widget the class must extend the WidgetAbstract class and override the __invoke, controller, model and view methods,

Usage

Simple hello world

$widget = new \MHDev\WidgetCore\MH_Widget("\MHDev\Widgets\\");
echo $widget("Example\HelloWorld");

Gravatar

$widget = new \MHDev\WidgetCore\MH_Widget("\MHDev\Widgets\\");
echo $widget("Gravatar", ["email" => "info@matteohertel.com", "size" => 80]);

Example

To see the package in action run the following(php must be available in the console):

git clone https://github.com/matteo-hertel/MH_Widget.git
cd MH_Widget
php -S localhost:8080 -t ./Example

the open your browser to http://localhost:8080

Version

0.2

Feedback

Please, send me your feedback, using the issue tracker on github or via email to info[at]matteohertel.com

Dependencies

There are no external dependencies but the Parser require the native DOMDocument class and a the widget tag syntax must be right to work properly

License

MIT


  Files folder image Files (11)  
File Role Description
Files folder imageExample (2 files)
Files folder imageMHDev (2 directories)
Accessible without login Plain text file .gitignore Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Data Auxiliary data
Accessible without login Plain text file README.MD Doc. Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:164
This week:0
All time:8,925
This week:49Up