PHPackages                             inc2734/wp-view-controller - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. inc2734/wp-view-controller

ActiveLibrary[Templating &amp; Views](/categories/templating)

inc2734/wp-view-controller
==========================

A library for WordPress theme view controller.

12.4.9(3mo ago)47.9k↑112.5%1[4 PRs](https://github.com/inc2734/wp-view-controller/pulls)1GPL-2.0-or-laterPHPPHP &gt;=7.4CI passing

Since Jun 16Pushed 3mo agoCompare

[ Source](https://github.com/inc2734/wp-view-controller)[ Packagist](https://packagist.org/packages/inc2734/wp-view-controller)[ RSS](/packages/inc2734-wp-view-controller/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (9)Versions (99)Used By (1)

WP View Controller
==================

[](#wp-view-controller)

[![CI](https://github.com/inc2734/wp-view-controller/workflows/CI/badge.svg)](https://github.com/inc2734/wp-view-controller/workflows/CI/badge.svg)[![Latest Stable Version](https://camo.githubusercontent.com/5ca86c6cd5dc417216a2b371c9f73b6bed2a3b69497930b028628f3b782d93a2/68747470733a2f2f706f7365722e707567782e6f72672f696e63323733342f77702d766965772d636f6e74726f6c6c65722f762f737461626c65)](https://packagist.org/packages/inc2734/wp-view-controller)[![License](https://camo.githubusercontent.com/c9b2e6136c86e54f407b0179d72f501f79c744f6886db4be4420f246a62ed921/68747470733a2f2f706f7365722e707567782e6f72672f696e63323733342f77702d766965772d636f6e74726f6c6c65722f6c6963656e7365)](https://packagist.org/packages/inc2734/wp-view-controller)

Install
-------

[](#install)

```
$ composer require inc2734/wp-view-controller

```

How to use
----------

[](#how-to-use)

```
`.

View templates
--------------

[](#view-templates)

### In singular page

[](#in-singular-page)

Loading `/templates/view/content-{post-type}.php` for the view template. Loading `/templates/view/content.php` when `/templates/view/content-{post-type}.php` isn't exists.

### In archive page

[](#in-archive-page)

Loading `/templates/view/archive-{post-type}.php` for the view template. Loading `/templates/view/archive.php` when `/templates/view/archive-{post-type}.php` isn't exists.

### Static view templates

[](#static-view-templates)

Tries to load the view template according to the URL. For example when URL is , tries to laod from `/templates/static/foo/bar.php` or `/templates/static/foo/bar/index.php`.

Using view controller
---------------------

[](#using-view-controller)

```
use Inc2734\WP_View_Controller\Bootstrap;

Bootstrap::layout( 'right-sidebar' );
Bootstrap::render( 'content', 'news' );

```

Template tags
-------------

[](#template-tags)

### \\Inc2734\\WP\_View\_Controller\\Helper::get\_template\_part()

[](#inc2734wp_view_controllerhelperget_template_part)

This method is an extension to `get_template_part()`.

Filter hooks
------------

[](#filter-hooks)

### inc2734\_wp\_view\_controller\_config\_path

[](#inc2734_wp_view_controller_config_path)

Deprecated. Use `inc2734_wp_view_controller_config` whenever possible. When using a custom config file, return a plain array from the file. `inc2734_wp_view_controller_config` is applied after loading.

```
/**
 * Change config path
 *
 * @param string $path
 * @return string $path
 */
add_filter(
	'inc2734_wp_view_controller_config_path',
	function( $path ) {
		return $path;
	}
);

```

### inc2734\_wp\_view\_controller\_pre\_template\_part\_render

[](#inc2734_wp_view_controller_pre_template_part_render)

```
/**
 * Define and return the content before loading the template.
 * If a string is returned, the template will not load.
 *
 * @param null $html
 * @param string $slug
 * @param string $name
 * @param array $vars
 * @return string
 */
add_filter(
	'inc2734_wp_view_controller_pre_template_part_render',
	function( $html, $slug, $name, $vars ) {
		return $html;
	},
	10,
	4
);

```

### inc2734\_wp\_view\_controller\_template\_part\_render

[](#inc2734_wp_view_controller_template_part_render)

```
/**
 * Customize the rendered HTML
 *
 * @param string $html
 * @param string $slug
 * @param string $name
 * @param array $vars
 * @return string
 */
add_filter(
	'inc2734_wp_view_controller_template_part_render',
	function( $html, $slug, $name, $vars ) {
		return $html;
	},
	10,
	4
);

```

### inc2734\_wp\_view\_controller\_layout

[](#inc2734_wp_view_controller_layout)

```
/**
 * Change layout file
 *
 * @param string $layout The layout file template slug
 * @return string
 */
add_filter(
	'inc2734_wp_view_controller_layout',
	function( $layout ) {
		return $layout;
	}
);

```

### inc2734\_wp\_view\_controller\_view

[](#inc2734_wp_view_controller_view)

```
/**
* Change view file
*
* @param string $view The view file template slug
* @return string
*/
add_filter(
	'inc2734_wp_view_controller_view',
	function( $view ) {
		return $view;
	}
);

```

### inc2734\_wp\_view\_controller\_config

[](#inc2734_wp_view_controller_config)

This filter always runs after loading the config, including when `inc2734_wp_view_controller_config_path` points to a custom config file.

```
/**
 * Customize config values
 *
 * @param array $values
 * @return array
*/
add_filter(
	'inc2734_wp_view_controller_config',
	function( $values ) {
		return $values;
	}
);

```

### inc2734\_wp\_view\_controller\_controller

[](#inc2734_wp_view_controller_controller)

```
/**
 * Change controller
 *
 * @param string $template
 * @return string
 */
add_filter(
	'inc2734_wp_view_controller_controller',
	function( $template ) {
		return $template;
	}
);

```

### inc2734\_wp\_view\_controller\_get\_template\_part\_args

[](#inc2734_wp_view_controller_get_template_part_args)

```
/**
 * Customize template part args
 *
 * @param array $args
 *   @var string $slug
 *   @var string $name
 *   @var array $vars
 * @return array
 */
add_filter(
	'inc2734_wp_view_controller_get_template_part_args',
	function( $args ) {
		return $args;
	}
);

```

### inc2734\_wp\_view\_controller\_template\_part\_root\_hierarchy

[](#inc2734_wp_view_controller_template_part_root_hierarchy)

```
/**
 * Customize root hierarchy
 *
 * @param array $hierarchy
 * @param string $slug
 * @param string $name
 * @param array $vars
 * @return array
 */
add_filter(
	'inc2734_wp_view_controller_template_part_root_hierarchy',
	function( $hierarchy, $slug, $name, $vars ) {
		return $hierarchy;
	},
	10,
	4
);

```

### inc2734\_wp\_view\_controller\_located\_template\_slug\_fallback

[](#inc2734_wp_view_controller_located_template_slug_fallback)

```
/**
 * You can specify a template for fallback
 *
 * @param string|null $fallback_slug
 * @param array $relative_dir_paths
 * @param string $slug
 * @param string $name
 * @return string|null
 */
add_filter(
	'inc2734_wp_view_controller_located_template_slug_fallback',
	function( $fallback_slug, $relative_dir_paths, $slug, $name ) {
		return $fallback_slug;
	},
	10,
	4
);

```

### inc2734\_wp\_view\_controller\_render\_type

[](#inc2734_wp_view_controller_render_type)

```
/**
 * Change rendered type.
 *
 * loop ... Loading in the WordPress loop
 * direct ... Simply loading
 *
 * @param string $render_type loop or direct
 * @return string
 */
add_filter(
	'inc2734_wp_view_controller_render_type',
	function( $render_type ) {
		return $render_type;
	}
);

```

Action hooks
------------

[](#action-hooks)

### inc2734\_wp\_view\_controller\_get\_template\_part\_&lt;slug&gt;-&lt;name&gt;

[](#inc2734_wp_view_controller_get_template_part_slug-name)

```
/**
 * Define template
 *
 * @deprecated
 *
 * @param array $vars
 * @return void
 */
add_action(
	'inc2734_wp_view_controller_get_template_part_-',
	function( $vars ) {
		?>
		HTML

		HTML
