📜 ⬆️ ⬇️

First look at Themosis, a framework for WordPress developers



More recently, I heard about a new project called Themosis , a “framework for WordPress developers.” In this article we will explore its capabilities and consider an example - a site created by the developers of Themosis. The most interesting is hidden behind the cut.

What is Themosis?



For anyone familiar with the word “framework,” especially regarding PHP, this may sound surprising. Is WP really lacking blogging features? In general, yes.
')
Themosis is not a framework in the sense in which you understand it. This is an API for easy site creation. Themosis is a whole set of APIs for developing WordPress components with classes, anonymous functions and Composer support.

Themosis itself is a plugin for writing other plugins. Curious isn't it? It has a tool for creating paths, as well as its own template engine - Scout. It is designed specifically to work with WordPress. For example, pattern loop:

@loop(array('post_type' => 'post', 'posts_per_page' => -1)) <h1>{{ Loop::title() }}</h1> <div> {{ Loop::content() }} </div> @endloop 


uses the API to request information, as well as the Loop class.

Installation


Installation is done using Composer and the WordPress command line tool. When you create a Composer project for Themosis, you need to perform a standard WP installation. After that, you can start working with the framework.

You can follow the official installation instructions for running your first example below.

Sample project



Themosis suggests to try an example from their official site - a site for selling books .

Attempt at writing


You can use this tip to get started with the system. Then simply register the site with:

 - map: bookstore.local to: /home/vagrant/Code/themosis_example 


Of course, do not forget to add the bookstore.local alias to your hosts file as described here .

Clone the repository with the command git clone github.com/themosis/bookstore themosis_example git clone github.com/themosis/bookstore themosis_example , and go to bookstore.local:8000 bookstore.local:8000 in your browser for installing WordPress.

After completing the installation, follow the remaining instructions written here . Now you can do whatever you want, and not be afraid that something will go wrong. At any time, you can rebuild the VM and start the process again.

After completing all of the above instructions, you can open the Themosis sample application.



At the moment, you will not notice anything interesting, since any WordPress site can be customized to look like the example below. However, at the moment I want to highlight two key points:

  1. We started this project very quickly, practically doing nothing;
  2. Themosis uses the WordPress API and processes requests extremely quickly.


What about the code? What does he look like? Let's explore this question in the next paragraph.

Actually, the code


You can view the code in the sample project repository: https://github.com/themosis/bookstore . Let's see what we can point out here. As I said before, Themosis is a plugin, so consider the plugin code for a book site:



Here we see three folders: an app containing the logical part of our site, src containing the source code Themosis (the folder structure resembles other frameworks, and their names correspond to the classes), and vendor is the place where Composer works.

Speaking about the quality of the code, one can note the observance of most modern standards (names, classes), as well as the use of special code to restrict access to PHP.

However, this is not the only place where Themosis code is contained. The entire logical part is contained in the app, and the template is in other folders.



MVC


Looking at the structure of MVC, I can say that it is well done. It would be nice if it were not divided into fragments (in several parent folders), but we will not change anything ourselves. As in Laravel, the folder with the paths is located in the app folder; a special syntax is supported. Our example also uses controllers. From home.controller.php :

 return View::make('pages.home')->with(array( 'promo' => Books::getPromoBook($this->page), 'books' => Books::getPopularBooks($this->bookId), 'news' => News::get(), 'newspage' => get_page_by_path('news') )); 


Routing with parameters


Looking at some code fragments, it is impossible not to be surprised by their simplicity. Consider, for example, a search page. This page is so simple that it doesn’t even need a controller. She simply refers to the search engine WP ...

 //   Route::is('search', function(){ return View::make('search', array('search' => $_GET['s'])); }); 


I like it not only because of its simplicity, but also because the WP search system — which is usually not the best — can be easily replaced.

More routing information can be found here . Still, as an option, you can see examples.

Creating a page with its own controller


Let's find out if it's really about creating pages as easy as they say.

First, we will write all the routes into the routes.php file:

 Route::only('page', 'test', 'test@index'); 


After creating a new file named est.scout.php :

 @include('header') Hello World @include('footer') 


And finally, let's create a controller in the controllers folder:

 <?php class Test_Controller{ /** *    . * * @return object. */ public function index(){ return View::make('pages.test'); } } 


In theory, this should output “hello world” for the /test route in any framework, but due to the fact that Themosis works with the WordPress API, we need to perform one more step. Let's add a page to the system to make the route work.



After the reboot, we get our Hello world.



But we wrote “Is anyone there?” In the content of the page, not “Hello World”! Why did it happen? To include our content on the page, we need to access the $post global variable in our controller.

If we change our controller to work with content:

 <?php class Test_Controller{ /** *     . * * @return object. */ public function index(){ global $post; return View::make('pages.test')->with(array( 'post' => $post )); } } 


and add something to the view:

 @include('header') <h1>Page Content: {{ $post->post_content;}} </h1> @include('footer') 


... then we get what we needed:



Difficult? It is possible that slightly, but I think you can get used to this. The important thing here is that we made it possible to process all the information of WordPress.

Conclusion



Thinking about developing with the help of the Themosis plug-in, we can say that it would not be needed if the WP core were more perfect. Themosis creates the necessary structure and removes unnecessary chaos, with which we had to reconcile.

Themosis is still at the very beginning of development, being in version 0.8, but not until the release of 1.0 is so far, and I wish the developer good luck and success in his project.

Source: https://habr.com/ru/post/229653/


All Articles