krafthaus / bauhausblock by jspekken

Simple block handling for laravel
26
0
2
Package Data
Maintainer Username: jspekken
Maintainer Contact: hello@krafthaus.nl (KraftHaus)
Package Create Date: 2014-07-08
Package Last Update: 2014-09-02
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-24 03:03:08
Package Statistics
Total Downloads: 26
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 2
Total Forks: 0
Total Open Issues: 1

Bauhaus Block - Rendering of block elements

Latest Stable Version Latest Unstable Version Total Downloads License

Handle rendering of block element. A block is a small unit with its own logic and templates. A block can be inserted anywhere in a current laravel template.

Installation

Add bauhaus block to your composer.json file:

"require": {
	"krafthaus/bauhausblock": "dev-master"
}

Use composer to install this package

$ composer update

Register the package

'providers' => array(
	'KraftHaus\BauhausBlock\BauhausBlockServiceProvider'
),

'aliases' => array(
	'Block' => 'KraftHaus\BauhausBlock\Facades\Block'
)

Add the blocks folder to the app/ directory and put the following line in your composer.json file:

"autoload": {
	"classmap": [
		"app/blocks"
	]
}

Then run $ composer dump-autoload and you're done.

Your first block

This quick tutorial explains how to create a RSS reader block. A block is nothing more than a class defining properties that renders a block partial.

Every block you create for your project should be located in the app/blocks folder. So lets create an RssBlock.php file in that folder with the following contents:

<?php

use KraftHaus\BauhausBlock\Block;
use KraftHaus\BauhausBlock\Resolver\OptionResolver;

class RssBlock extends Block
{

    /**
     * Configure the block with this method.
     * Use the OptionResolver instance to set or get options for this block.
     */
	public function configure(OptionResolver $resolver)
	{
		$resolver
			->set('url',  'https://github.com/krafthaus/bauhaus/commits/master.atom')
			->set('view', 'blocks.rss');
	}

    /**
     * This method is called every time a block is rendered and used to
     * initialize the block with data.
     */
	public function execute()
	{
		$options = $this->getOptionResolver();

		$content = file_get_contents($options->get('url'));
		$feed    = simplexml_load_string($content);

		$options->set('feed', $feed->entry);

		return $this;
	}

}

Create the view for this block at app/views/blocks/rss.blade.php with the following contents:

<ul>
    @foreach ($options->feed as $item)
        <li>{{ $item->title }}</li>
    @endforeach
</ul>

To render the block just call:

{{ Block::render('rss') }}

That's it!

Support

Have a bug? Please create an issue here on GitHub that conforms with necolas's guidelines.

License

This package is available under the MIT license.