2017 © Pedro Peláez
 

package sagecontroller

WordPress package to enable a basic controller when using Timber/Twig with Sage 9

image

itcig/sagecontroller

WordPress package to enable a basic controller when using Timber/Twig with Sage 9

  • Wednesday, June 13, 2018
  • by itcig
  • Repository
  • 3 Watchers
  • 2 Stars
  • 35 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 21 Forks
  • 0 Open issues
  • 20 Versions
  • 25 % Grown

The README.md

Controller

WordPress package to enable a controller when using Timber/Twig with Sage 9. This also requires heavy modification of Sage 9 to use., (*1)

Installation

Composer:

Please note that Controller is no longer an mu-plugin and is now a Composer theme depedency., (*2)

Browse into the Sage theme directory and run;, (*3)

$ composer require itcig/sagecontroller:2.0.2

Requirements:

Setup

By default, create folder app/Controllers/ within your theme directory., (*4)

Alternatively, you can define a custom path using the filter below within your themes resources/functions.php file;, (*5)


add_filter('sober/controller/path', function () { return dirname(get_template_directory()) . '/app/Custom-folder'; });

The controller will autoload PHP files within the above path and its subdirectories., (*6)

Usage

Creating a basic Controller:

  • Controller files follow the same hierarchy as WordPress.
  • Extend the Controller Class— it is recommended that the class name matches the filename.
  • Create methods within the Controller Class;
    • Use public function to expose the returned values to the Twig views/s.
    • Use public static function to use the function within your Twig view/s.
    • Use protected function for internal controller methods as only public methods are exposed to the view. You can run them within __construct.
  • Return a value from the public methods which will be passed onto the Twig view.
    • Important: The method name is converted to snake case and becomes the variable name in the Twig view.
    • Important: If the same method name is declared twice, the latest instance will override the previous.

Examples:

The following example will expose $images to resources/views/single.twig, (*7)

app/controllers/Single.php, (*8)

<?php

namespace App;

use Cig\Sage\Controller\Controller;

class Single extends Controller
{
    /**
     * Return images from Advanced Custom Fields
     *
     * @return array
     */
    public function images()
    {
        return get_field('images');
    }
}

resources/views/single.twig, (*9)

{% if(images|length) %}
  <ul>
    {% for image in images %}
      <li><img src="{{ image.sizes.thumbnail }}" alt="{{ image.alt }}"></li>
    {% endfor %}
  </ul>
{% endif %}

Creating Components;

You can also create reusable components and include them in a view using PHP traits., (*10)

app/controllers/partials/Images.php, (*11)

<?php

namespace App;

trait Images
{
    public function images()
    {
        return get_field('images');
    }
}

You can now include the Images trait into any view to pass on variable $images;, (*12)

app/controllers/Single.php, (*13)

<?php

namespace App;

use Cig\Sage\Controller\Controller;

class Single extends Controller
{
    use Images;
}

Using Static Methods;

You can use static methods as a pass-thru method that returns content from your controller., (*14)

This is useful if you are within the loop and want to return data for each post item individually by passing in a $post_id., (*15)

app/controllers/Archive.php, (*16)

<?php

namespace App;

use Cig\Sage\Controller\Controller;

class Archive extends Controller
{
    public static function callback_method($arg = null)
    {
        return my_callback($arg);
    }
}

resources/views/archive.php, (*17)

{% extends "base.twig" %}

{% block content %}
    {{ callback_method() }}
{% endblock %}

Inheriting the Tree/Heirarchy;

By default, each Controller overrides its template heirarchy depending on the specificity of the Controller (the same way WordPress templates work)., (*18)

You can inherit the data from less specific Controllers in the heirarchy by implementing the Tree., (*19)

For example, the following app/controllers/Single.php example will inherit methods from app/controllers/Singular.php;, (*20)

app/controllers/Single.php, (*21)

<?php

namespace App;

use Cig\Sage\Controller\Controller;
use Cig\Sage\Controller\Module\Tree;

class Single extends Controller implements Tree
{

}

If you prefer you can also do this;, (*22)

<?php

namespace App;

use Cig\Sage\Controller\Controller;

class Single extends Controller
{
    protected $tree = true;
}

You can override a app/Controllers/Singular.php method by declaring the same method name in app/Controllers/Single.php;, (*23)

Creating Global Properties;

Methods created in app/Controllers/App.php will be inherited by all views and can not be disabled as resources/views/layouts/app.php extends all views., (*24)

app/Controllers/App.php, (*25)

<?php

namespace App;

use Cig\Sage\Controller\Controller;

class App extends Controller
{
    public function siteName()
    {
        return get_bloginfo('name');
    }
}

Disable Option;

protected $active = false;

Twig Debugging;

Coming soon, (*26)

Updates

Composer:

  • Change the composer.json version to ^2.0.2
  • Check CHANGELOG.md for any breaking changes before updating.
$ composer update

WordPress:

Includes support for github-updater to keep track on updates through the WordPress backend. * Download github-updater * Clone github-updater to your sites plugins/ folder * Activate via WordPress, (*27)

Special Thanks

  • Most of the leg work here was done by Daren Jacoby so show him some love
  • For Controller updates and other WordPress dev, follow @withjacoby

The Versions

13/06 2018

dev-master

9999999-dev https://github.com/itcig

WordPress package to enable a basic controller when using Timber/Twig with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

13/06 2018

2.0.2

2.0.2.0 https://github.com/itcig

WordPress package to enable a basic controller when using Timber/Twig with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

11/06 2018

9.0.0-beta.5

9.0.0.0-beta5 https://github.com/itcig

WordPress package to enable a basic controller when using Timber/Twig with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

22/08 2017

9.0.0-beta.4

9.0.0.0-beta4 https://github.com/soberwp

WordPress package to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

03/06 2017

9.0.0-beta.3

9.0.0.0-beta3 https://github.com/soberwp

WordPress package to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

13/04 2017

9.0.0-beta2.1

9.0.0.0-beta2.1 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

04/04 2017

9.0.0-beta2

9.0.0.0-beta2 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

15/03 2017

1.0.2

1.0.2.0 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

14/03 2017

1.0.1

1.0.1.0 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

11/03 2017

1.0.0

1.0.0.0 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

28/02 2017

1.0.0-beta1.2

1.0.0.0-beta1.2 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

28/02 2017

1.0.0-beta1.1

1.0.0.0-beta1.1 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

27/02 2017

1.0.0-beta1

1.0.0.0-beta1 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

26/02 2017

1.0.0-alpha7

1.0.0.0-alpha7 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

25/02 2017

1.0.0-alpha6

1.0.0.0-alpha6 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

23/02 2017

1.0.0-alpha5

1.0.0.0-alpha5 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

23/02 2017

1.0.0-alpha4

1.0.0.0-alpha4 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

22/02 2017

1.0.0-alpha3

1.0.0.0-alpha3 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

18/02 2017

1.0.0-alpha2

1.0.0.0-alpha2 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress

17/02 2017

1.0.0-alpha1

1.0.0.0-alpha1 https://github.com/soberwp

WordPress plugin to enable a basic controller when using Blade with Sage 9

  Sources   Download

MIT

The Requires

 

The Development Requires

wordpress