Closed iniznet closed 2 years ago
Hi @iniznet Thanks a lot for this pull request. We need View system in this Package. I have a suggestion., please creating a feature for Overriding templates via a current active theme in WordPress,
// First Check User Overriding Template in your Theme
if(!file_exists(get_template_directory().'/my-plugin-slug/alert/success.php'){
// If not Exist File, then Use Plugin templates
include $this->path.'/templates/alert/success.php';
}
Example in WooCommerce: https://woocommerce.com/document/template-structure/#how-to-edit-files
Hi @iniznet Thanks a lot for this pull request. We need View system in this Package. I have a suggestion., please creating a feature for Overriding templates via a current active theme in WordPress,
// First Check User Overriding Template in your Theme if(!file_exists(get_template_directory().'/my-plugin-slug/alert/success.php'){ // If not Exist File, then Use Plugin templates include $this->path.'/templates/alert/success.php'; }
Example in WooCommerce: https://woocommerce.com/document/template-structure/#how-to-edit-files
Done! What do you think?
/** src/admin.php */
public function admin_notices()
{
$text = __('This Notice is a example from your plugin', $this->plugin->textdomain);
// First Way
$content = $this->view()
->attribute('text', $text)
->attribute([
'description' => __('This is the description of your notice', $this->plugin->textdomain)
])
->render('notice');
echo $this->add_alert($content, 'info');
// Second Way
$content = $this->view()->render('notice', [
'text' => $text
], [
'text' => __('Actually this is the real notice example from your plugin', $this->plugin->textdomain)
]);
echo $this->add_alert($content, 'info');
// Third Way
$content = $this->view();
$content->text = $text;
echo $this->add_alert($content('notice'), 'info');
// Property Way
echo $this->add_alert($this->view->render('notice', [
'text' => $text . ' with property way'
]), 'info');
// Property Way with overriden view
echo $this->add_alert($this->view->render('notice-override', [
'text' => $text . ' with property way'
]), 'info');
}
<!-- themes/generatepress/plugin-slug/templates/notice-override.php -->
<strong style="display: block;"><?= $text . ' and overriden view'; ?></strong>
<?php if (isset($description)): ?>
<small><?= $description; ?></small>
<?php endif; ?>
~I think I need some more tests before we merge this. My latest commit breaks the overriding feature due to wanting to fix the open_basedir restrictions error because when a file doesn't exist it returns /
path.~
It's good to go now.
I think adding a new parameter in render
method for example $canOverride
with boolean
value and default is true
. that
Developer allow to user for overriding custom php file or not.
That is a good idea?
I think so, it's necessary to have it so that the Developer is able to toggle their templates overriding.
/** src/admin.php:admin_notices() */
// Property Way with overriden view
echo $this->add_alert($this->view->render('notice-override-off', [
'text' => $text . ' with property way and active theme should not override this view'
], [], false), 'info');
<!-- themes/generatepress/plugin-slug/templates/notice-override-off.php -->
<strong style="display: block;"><?= $text . ' but actually active theme can override this view'; ?></strong>
<?php if (isset($description)): ?>
<small><?= $description; ?></small>
<?php endif; ?>
we doNot need templates
in current theme dir.
please replace:
$paths = [
get_stylesheet_directory() . '/' . $this->plugin->slug . '/templates',
get_template_directory() . '/' . $this->plugin->slug . '/templates',
];
To
$paths = [
get_stylesheet_directory() . '/' . $this->plugin->slug,
get_template_directory() . '/' . $this->plugin->slug,
];
That will break the overriding feature because I'm setting:
protected function setPath($path = '', $plugin = null)
{
if (!$path && $plugin) {
$path = $plugin->path . 'templates';
}
$this->path = $path;
}
It will try to look: themes/generatepress/plugin-slug/notice-override.php
currently:
themes/generatepress/plugin-slug/templates/notice-override.php
plugins/plugin-slug/templates/notice-override.php
My bad, after rechecking the woocommerce docs you're right.
I push this request in master branch. Please Help me for writing document View Template in Readme.md. Thanks
Taken & modified from: Generate Model manually
By default the template path:
plugin-directory/templates